函数在python中不工作

2024-06-10 20:53:00 发布

您现在位置: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)?")

不断得到的错误,果汁或水果的输入没有定义


Tags: andthe代码inputmake编程anotherjuice
2条回答

只是一个猜测。。。如果您使用的是python2,那么使用raw_input而不是input。。。(在python2中,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"

它对我来说工作得很好,我使用的是python2.x。您提供果汁和水果的数字是因为您使用%d进行文本格式化吗?你知道吗

oltjano@baby:~/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)?

相关问题 更多 >