2013-02-13 75 views
3

我正在Python中猜測玩家選擇的數字。TypeError:最多輸入1個參數,得到3

# Computer Guessing Game 
# The computer tries to guess your number 

print("Think of a number, and I will try to guess it. If my guess is right,") 
print("say 'yes'.If my guess is too high, say 'lower'. And if my guess is") 
print("too low, say 'higher'.\n") 

answer = input("Is it 50? ") 
guess = 50 

while answer != "yes": 
    hilo = input("Is it higher or lower? ") 
    if hilo == "lower": 
     guess %= 50 
     answer = input("Is it", guess, "?") 
    if hilo == "higher": 
     guess %= 150 
     answer = input("Is it", guess, "?") 

print("I win!") 

input("Press the enter key to exit.") 

但是,在運行時,線15和代碼的18

answer = input("Is it", guess, "?") 

回報「類型錯誤:輸入預計最多1個參數,得到3」我不知道如何解決這個問題,所以任何幫助將不勝感激。

回答

5

input只接受一個的說法,你是傳遞3.您需要使用字符串格式化或串聯,使其一個參數:

answer = input("Is it {} ?".format(guess)) 

您與print()功能,這確實是這個混亂確實需要多個參數,並將值連接成一個字符串給你。

+0

謝謝Martijn。我會嘗試。 – Francois 2013-02-13 15:30:48

相關問題