2014-11-06 66 views
0

編程的新手。試圖使用Head First Programming書自學。 我無法得到以下代碼來工作;函數不能在Python中工作

  def make_smoothie(): 
       juice = input("What juice would you like?") 
       fruit = input("Ok- and how about the fruit?") 
       print "Thanks. Lets go!" 
       print "Crushing ice..." 
       print "Blending the %d" % fruit 
       print "Now adding in the %d juice" %juice 
       print "Finished! There's your %d and %d smoothie" %(fruit, juice) 


      print ("Welcome to smoothie") 
      another ="Y" 
      while another=="Y": 
       make_smoothie() 
       another = input ("How about another (Y/N)?") 

不斷得到對於果汁或水果的輸入沒有定義

+1

嘗試用'%s'替換%D'的'實例 – MattDMo 2014-11-06 00:56:05

+0

同樣的錯誤,對於果汁輸入沒有被定義。 – 2014-11-06 01:01:20

+0

看到我的答案...它的原因,代碼是爲python3,但你使用python 2 ...和用戶輸入方法有點不同 – 2014-11-06 01:02:46

回答

0

只是一個猜測...如果你正在使用python 2然後用raw_input代替input ...(在Python 2中的錯誤input將嘗試評估輸入到Python對象(整數或變量名等),用戶... raw_input將返回用戶輸入的字符串)

考慮

>>> apple = 555 
>>> print input("Enter Fruit:") 
Enter Fruit:apple 
555 #returns the value of the variable apple ... if that does not exist you get an error 
>>> print raw_input("Enter Fruit:") 
Enter Fruit:apple 
apple #returns the string "apple" 
1

它適用於我,我使用Python 2.x.你是否提供果汁和水果的數字,因爲你使用%d來進行文本格式化?

[email protected]:~/Desktop/unveil/tests$ python juice.py 
Welcome to smoothie 
How about another (Y/N)?"Y" 
What juice would you like?1 
Ok- and how about the fruit?2 
Thanks. Lets go! 
Crushing ice... 
Blending the 2 
Now adding in the 1 juice 
Finished! There's your 2 and 1 smoothie 
How about another (Y/N)? 
+0

+1指出,即使他修復輸入它仍然會打破他的格式字符串 – 2014-11-06 01:16:37