2016-04-02 11 views
-1

這是我的代碼:我已經做了猜測的數字遊戲,但我每次運行它,並鍵入一些時間這個崩潰

#This is a guess the number game 
import random 

print('Hello what is your name') 
name = input() 
secretNumber = random.randint(1, 20) 
print('Well ' + name + ' I am thinking of a number between 1 and 20. You have 6 guesses') 

#Ask the player to guess 6 times. 
for guessTaken in range(1, 7): 

    try: 
     print('Take a guess') 
     guess = int(input()) 
    except ValueError: 
      print('That is not a number') 

    guess = int(input()) 
    guess = str(input()) 
    if guess < secretNumber: 
     print('Your guess is to low.') 
    elif guess > secretNumber: 
     print('Your gues s is to high.') 
    else: 
     break # This condition is the correct guess! 

if guess == secretNumber: 
    print('Good job ' + name + '. You guessed my number!') 
    print('It was ' + str(secretNumber)) 
    print('and you guessed it in ' + str(guessTaken) + ' guesses') 
else: 
    print('Nope. The number I was thinking of was ' + str(secretNumber)) 

當過我運行它,並鍵入一些這樣出現:猜= INT(輸入()) ValueError異常:無效的字面INT()基數爲10: ''

+1

在guess = int(input())之後,你會怎麼想'guess = str(input())'? –

回答

0

試試這個:

#This is a guess the number game 
import random 


name = input('Hello what is your name: ') 
secretNumber = random.randint(1, 20) 
print('Well, ' + name + ' I am thinking of a number between 1 and 20. You have 6 guesses') 

#Ask the player to guess 6 times. 
for guessTaken in range(1, 7): 

    try: 
     guess = int(input('Take a guess: ')) 
    except ValueError: 
      print('That is not a number') 

    if guess < secretNumber: 
     print('Your guess is to low.') 
    elif guess > secretNumber: 
     print('Your guess is to high.') 
    else: 
     break # This condition is the correct guess! 

if guess == secretNumber: 
    print('Good job ' + name + '. You guessed my number!') 
    print('It was ' + str(secretNumber)) 
    print('and you guessed it in ' + str(guessTaken) + ' guesses') 
else: 
    print('Nope. The number I was thinking of was ' + str(secretNumber)) 

您完全沒有必要:

guess = int(input()) 
    guess = str(input()) 
+0

謝謝你的分配:-) – Scythe

+1

你的代碼會崩潰,如果你得到一個錯誤,哪裏會猜測定義?,輸入也需要一個參數,所以你不需要打印 –

0

我不認爲你想

guess = int(input()) 
guess = str(input()) 
在循環

。嘗試擺脫這些(你只需要在該循環中進行一次用戶輸入,而不是三次)。不過,請將guess = int(input())保留在try區塊內。

相關問題