2016-07-22 77 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

您還沒有正確縮進while循環,作爲一個經驗法則,像ifwhile語句需要一個新的生產線和壓痕之後。

所以這可能是更好的代碼使用方法:

# 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") 

那你不需要chk的所有方式。

+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