0

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

5
  • 1
    Try replacing the instances of %d with %s Commented Nov 6, 2014 at 0:56
  • Same error, input for juice is not defined. Commented Nov 6, 2014 at 1:01
  • see my answer ... its cause that code is meant for python3 but you are using python 2 ... and the user input methods are a little difference Commented Nov 6, 2014 at 1:02
  • Done! Worked. Thanks heaps Commented Nov 6, 2014 at 1:05
  • Please mark at least one answer as accepted so person gets credit and so browswer like me know that you aoready have answer. Commented Nov 6, 2014 at 3:36

2 Answers 2

1

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)?
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for pointing out that even if he fixes input it will still break on his format string
0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.