2014-11-06 68 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