2016-10-15 12 views
0

私はゲームの数字の種類を推測するようになりました。私はプログラムを実行すると、それはどちらか、完全に流れ、動作しないか...スクリプトを実行するときの奇妙な振る舞い

import random 
from random import randint 

print("Welcome to guess the number!\nDo you want to play the game?") 

question = input("") 

if question == "Yes".lower(): 
print("Sweet! Let`s begin!\nGuess the number between 1 and 10!") 

    number = random.randint(1, 10) 
    guess = int(input("Take a guess!\n")) 

    if guess > number: 
     print("Your guess is too high") 
     guess = int(input("Take a guess!\n")) 

    if guess < number: 
     print("Your guess is too low") 
     guess = int(input("Take a guess!\n")) 

    if guess == number: 
     print("Your guess was correct!") 

elif question == "No".lower(): 
    print("Too bad! Bye!") 
    quit() 

私絶対にそれがあるため、コードの発生した、またはpycharmは責任があるかどうか全くわかりません!

+2

こんにちは、偉大なプログラムの世界へようこそ。私たちの世界では、例外が発生した場合、詳細な説明とスタックトレースで表現されています。 – spectras

+0

'print( 'Sweet' ...'はインデントされている必要があり、 'question =="ならばそれを変更したい ".lower()' 'question.lower()==" yes "'。 – Jaco

答えて

0

本当にwhileループが必要です。最初の推測が高すぎる場合、別のチャンスが得られますが、それはそれがあまりにも低いか等しいかどうかを調べるだけです。あなたの推測があまりにも低い場合は、2回目のチャンスが正しいことが必要です。

テストを続ける必要はないので、単純化することができますが、実際には設計段階でこれを行う必要があります。ここに私のバージョンは次のとおりです。

import random 
# from random import randint << no need for this line 

print("Welcome to guess the number!") 

question = input("Do you want to play the game? ") 

if question.lower() == "yes": 
    print("Sweet! Let`s begin!\nGuess the number between 1 and 10!") 

    number = random.randint(1, 10) 
    guess = None      # This initialises the variable 

    while guess != number:   # This allows continual guesses 

     guess = int(input("Take a guess!: ")) # Only need one of these 

     if guess > number: 
      print("Your guess is too high") 
     elif guess < number:  
      print("Your guess is too low") 
     else: # if it isn't <or> then it must be equal! 
      print("Your guess was correct!") 

else: 
    # why do another test? No need. 
    print("Too bad! Bye!") 

    # No need for quit - program will exit anyway 
    # but you should not use quit() in a program 
    # use sys.exit() instead 

今、私はあなたが、彼らはそれが権利を取得する前にプレイヤーが持っている推測の数のカウントを追加し、最後にあることを印刷示唆します!

編集:私のimportのステートメントは@Denis Ismailajのステートメントとは異なります。 importが1つしか必要ではないが、どちらの意見が異なっているかはどちらも同意する。私のバージョンでは、私はimport randomで、これはrandom.randintと言う必要がありますが、他方ではrandintとしか言いようがありません。

小さなプログラムでは、あまり選択する必要はありませんが、プログラムは決して小さくなりません。 6,7,8以上のモジュールを簡単にインポートできる大きなプログラムでは、関数がどのモジュールから来るのかを追跡することが難しい場合があります。これは名前空間汚染として知られています。 randintのようなよく知られている関数と混乱することはなく、関数の名前を明示的に指定すると簡単に戻すことができます。それは個人的な好みとスタイルの唯一の質問です。推測の数と

が追加:ここで

import random 

print("Welcome to guess the number!") 

question = input("Do you want to play the game? ") 

if question.lower() == "yes": 
    print("Sweet! Let`s begin!\nGuess the number between 1 and 10!") 

    number = random.randint(1, 10) 
    guess = None 
    nrGuesses = 0     # Added 

    # Allow for continual guesses 
    while guess != number and nrGuesses < 6: # Changed 

     guess = int(input("Take a guess!: ")) # Only need one of these 

     nrGuesses += 1    # Added 

     if guess > number: 
      print("Your guess is too high") 
     elif guess < number: 
      print("Your guess is too low") 
     else: # if it isn't <or> then it must be equal! 
      print("Your guess was correct!") 

else: 
    print("Too bad! Bye!") 

print("Number of guesses:", nrGuesses) # Added 
+0

os.exit()を使用するとこれが表示されます:この検査では解決する必要のある名前が検出されますが、動的ディスパッチとダックタイピングのために、私はsys.exit()を使用しようとしましたが、それは同じです。 – ImaNoob

+0

私は悪い、それは持っている必要がありますされた 'sys.exit()'おそらくPyCharmから来ているのでしょうか? 'quit()'は対話的なpythonでのみ使うべきです - http://stackoverflow.com/questions/6501121/difference-between-exit-and-sysを見てください-exit-in-python( 'exit()'と 'quit()'は同じことをします)。 – cdarke

+0

、しかし返されたnrGuessesは常に6です... Idk、これを解決する方法、任意のヒント? – ImaNoob

-1

はすでに答えとして提供2つのテーマのバリエーションです。
このオプションでは、推測部分は関数として定義されています。これにより、ユーザーが退屈になるまでゲームを繰り返しプレイするというオプションが提供されます。 もう1つのオプションは、ユーザーが「はい」または「いいえ」を入力する必要はなく、単に「y」または「n」で始まるものを入力することです。

from random import randint 

def game(): 
    print("Guess the number between 1 and 10!") 
    number = randint(1, 10) 
    guess = None 
    while guess != number: 
     guess = int(input("Take a guess!\n")) 
     if guess > number: 
      print("Your guess is too high") 
     elif guess < number: 
      print("Your guess is too low") 
     else: 
      print("Your guess was correct!") 

    return input("Another game y/n ") #return a value which determines if the game will be repeated 

print("Welcome to guess the number!\nDo you want to play the game?") 
question = input("") 
repeat = "y" # define repeat so that the first iteration happens 

if question.lower().startswith('y') == True: 
    print("Sweet! Let`s begin!") 
    while repeat.lower().startswith('y') == True: # while repeat == "y" keep playing 
     repeat = game() # play the game and set the repeat variable to what is returned 
print("Bye Bye!")