Newbie in programming. Trying to self-teach using Head First Programming book. I can not get this following code to work;
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)?")
Keep getting the error that the input for juice or fruit is not defined
2 Answers 2
It works fine for me and I am using Python 2.x. Are you providing numbers for juice and fruit because you use %d for text formatting?
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)?
1 Comment
just a guess ... if you are using python 2 then use raw_input instead of input ... (in python 2 input will try to evaluate the users input to a python object (integer or variable name, etc) ... raw_input will return the user input as a string)
consider
>>> 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"
%dwith%s