2017-09-17 109 views
1

我對與編程有關的所有事情都很陌生,剛剛完成了python課程的介紹並嘗試啓動一些項目。 我遇到了一些我找不到的東西。我的初學者項目出錯

lives=3 
while lives>0: 

    low=raw_input("what is the lower range that you will guess? Numbers only please.") 
    high=raw_input("what is the higher range that you will guess? Numbers only please") 
    thenumber=randint(int(low),int(high)) 
    if int(raw_input("pick an integer between %s and %s") %(low, high))==thenumber: 
     print "you won!" 

設置兩個變量爲「1」,它打印「挑%s和%s之間的整數」而不是「挑1和1之間的整數」之後。

編輯:提交的編號爲的猜測後,我也得到

TypeError: not all arguments converted during string formatting 
+0

一般情況下,嘗試谷歌搜索您收到的任何錯誤:https://www.google.it/search?client=safari&rls=zh-CN&q=TypeError:+not+all+arguments+converted+during+string+formatting&ie=UTF-8&oe=UTF-8&gfe_rd=cr& dcr = 0&ei = APa9Wc6ADq_CXuzysZgC –

回答

5

檢查括號:

raw_input("pick an integer between %s and %s") %(low, high) #bad 

raw_input("pick an integer between %s and %s" % (low, high)) #good 
+1

喜歡活潑的'#bad'和'#good'評論。 xD – Jerrybibo

+1

我認爲在開始時這個簡單的評論有所作爲... – kip

+1

不僅在一開始。你未來的自我會在後面踢你,因爲沒有對一個腳本進行評論...... :) –

2

%s取代是包住raw_input括號外;他們應該立即跟隨字符串(如在基普的回答中)。

只需使用.format語法。在我看來,更容易理解。

此:

"pick an integer between %s and %s" % (low, high) 

可以寫爲:

"pick an integer between {} and {}".format(low, high) 

你也可以做到這一點的Ruby風格(在Python 3.5+):

f"pick an integer between {low} and {high}"