-2
我想在Python中生成一個小程序,它會生成一個隨機數(從擲骰子中)並要求用戶猜測數字是什麼,以及如果用戶猜錯了,然後根據猜測是否高於實際數量或程序是否應該重新提示他們再次猜測。反覆詢問用戶輸入python
我在代碼中遇到的問題是,即使輸入了所有可能的值,輸出總是「有點高,請重試!」。
有人可以幫我找出代碼的哪個部分產生錯誤嗎?
import random
dice = random.randint(1,6)
answer = raw_input ("What do you think is the number?")
while (answer != dice):
if answer > dice:
print ("That's a little high. Try again!\n")
else:
print ("That's a little low. Try again!\n")
answer = raw_input ("What do you think is the number?")
print ("That's correct! Good job. \n")
謝謝!
[詢問用戶輸入的,直到他們得到一個有效的響應]的可能重複(http://stackoverflow.com/questions/23294658/asking-the-user-for輸入,直到他們給一個有效的迴應) –
你需要將'answer'轉換爲'int'。 – Matthias
另外:它看起來像你使用Python 2(來自'raw_input'),但將括號放在'print'中(就像你爲Python 3一樣)。我建議升級到Python 3,它會給你一個有用的錯誤消息,比如'TypeError:unorderable types:int()
DSM