2016-07-22 2 views
1

次のコードは、数字のゲームを推測するためのコードです。エラーはありませんが、出力は出ません。どうすればコードをさらに改善できますか?この数字の推測プログラムはなぜこの行を過ぎて続行しないのですか?

この行まではprint("Your Number has been Generated,Enter your guess Below")ですが、出力されません。

 #Guess the number 

    from random import randint 

    print("Welcome to Guess the Number Game:") 
    print("Press 'g' to Generate Number between 0 and 100:") 

    cmd=input("->") 

    if cmd=='g': 
     num=randint(0,100) 
    else: 
     print("Unknown Input") 


    def chk(x): 
     if int(x)==True: 
      chk=True 
     else: 
      chk=False 

    def diff(x,y): 
     diff=(x-y) 
     return diff 

    print("Your Number has been Generated,Enter your guess Below") 
    guess=input("->") 

    chk(guess) 

    while chk==True: 
    diff(num,guess) 
    if diff==0: 
    print("Congrats! correct Guess") 

    elif diff>10: 
    print("Oh! too high") 

    elif diff<5 and diff>0: 
    print("very close!Better Luck Next time") 

    else: 
    diff<0 
    print("Sorry!too Low") 

答えて

0

あなたはそのうちのいくつかは、問題を転写する、といくつかの問題があります。

def chk(x): 
    if int(x)==True: 
     chk=True 
    else: 
     chk=False 

私はこれがcheck入力に意味していると推定します。 Pythonのバージョンによっては、常にchkがfalseに設定されます。これは、intが整数を返します。これはブール型と同じ型ではありませんTrueです。あなたがここで達成しようとしていることは明確ではありません。入力が数字であることを確認したいように見えます。代わりになぜisdigitを使用しないのですか? 、現時点では、あなたのコードはfalseを返すので、あなたは

あなたはまた、経験則として、正しくwhileループをインデントしていない、ifwhile文のようなものが改行とインデントを必要とするwhileループに入ることはありませんその後。

は、これは、より良いコードを使用するかもしれません:あなたはまったくchk必要はありません

# keep guessing while it's a digit 
while guess.isdigit(): 
    # assign the difference to a variable 
    # don't forget to indent after the while statement 
    diff = diff(num, int(guess)) 
    if diff==0: 
     # indent again! 
     print("Congrats! correct Guess") 

    elif diff>10: 
     print("Oh! too high") 

    elif diff<5 and diff>0: 
     print("very close!Better Luck Next time") 

    elif diff<0: 
     print("Sorry!too Low") 

その通り。

+0

あなたが正しいです、私は入力が数字かどうかを確認するためにdef chk(x)を使用していましたが、私は親指のインデント規則を念頭に置いています。助けてくれてありがとう –

+0

私はchk(x)で考えていたのは、入力が数字以外の場合、つまり文字列がint(文字列)が不可能な場合、したがってint(x)== trueになります。偽、今私は理解している。洞察のおかげで –

+0

@RohanDwivedi実際に何が起こるかは、例外がスローされるということです。 – Pureferret

0

複数の問題があり、私は

from random import randint 

print("Welcome to Guess the Number Game:") 
print("Press 'g' to Generate Number between 0 and 100:") 

cmd=input("->") 

if cmd=='g': 
     num=randint(0,100) 
else: 
     print("Unknown Input") 


def chk(x): 
    if x.isnumeric(): # needs to be '.isNumeric()' as only the only integer that will return true is '1' 
      chk=True 
    else: 
      chk=False 
    return chk #Returns Boolean 

def diff(x,y): 
     return (y-x)# this needs to y-x to be correct 

print("Your Number has been Generated,Enter your guess Below") 
guess=input("->") 

while chk(guess): #or chk(guess) == True 
    difference = diff(num,int(guess))#guess needs to be an INTEGER 
    if difference==0: 
     print("Congrats! correct Guess") 
     exit() 

if difference>10: 
    print("Oh! too high") 

elif difference<5 and difference>0: 
    print("very close!Better Luck Next time") 

elif difference<0: 
    print("Sorry!too Low") 
print("Enter your guess Below")#allows you to put in another guess 
guess=input("->")#sets the guess variable 
chk(guess) 
1

良い答えはすでに与えられていますが、関数を定義せず、非常に「フラット」スクリプトでこれを行うことができ、コメントを使用してこれを示しています。これは例としてのみです:

from random import randint 

# welcome 
print("Welcome to Guess the Number Game:") 

# loop until 'g' is given 
cmd = '' 
while not cmd == 'g': 
    print("Press 'g' and '<enter>' to Generate Number between 0 and 100:") 
    cmd = input("->")  

# generates a random number 
num = randint(0,100) 
print("Your Number has been Generated,Enter your guess Below") 

# loops until correct answer is guessed 
while True: 
    try: 
     guess=int(input("guess->")) 
    except ValueError: 
     print('Numbers only please') 
     continue 
    if guess > num: 
     print('too high') 
    if guess < num: 
     print('too low')   
    if guess == num: 
     print("Congrats! correct Guess") 
     break 
関連する問題