[Tutor] Beginner level: Why doesn't my code work?

Rafael Knuth rafael.knuth at gmail.com
Mon May 20 18:47:04 CEST 2013


But this is all a distraction -- how exactly are you invoking what you think
is Python 3.3.0? What is your operating system?

My mistake, I am using two laptops with different operating systems
(Windows 7 & SUSE 12.3). I am using Python 3.3.0 on the Windows laptop and
I was wrongly assuming that I am running the same version of Python on my
SUSE laptop. I simply forgot that I have a Python 2.x version on my SUSE
laptop. I tried that game on my Windows 7 laptop (where I have 3.3.0
installed) and it runs nicely.

Thank you for your support!

import random

print("""

This is a magic super computer.

He will ask you a couple of questions.

An ultra-complicated algorithm will then figure out what your favorite meal
is.

""")

name = input("What is your name? ")

age = int(input("How old are you? "))

birthplace = input("Where are you born? ")

meal = random.randint(1, 3)

if meal == 1:
    print("Well, " + name + " as a " + str(age) + " year old human being
born in " + birthplace + " you probably like hamburgers.")

elif meal == 2:
    print("Well, " + name + " as a " + str(age) + " year old human being
born in " + birthplace + " you probably like sushi.")

elif meal == 3:
    print("Well, " + name + " as a " + str(age) + " year old human being
born in " + birthplace + " you probably like pizza.")


On Sun, May 19, 2013 at 4:54 PM, Peter Otten <__peter__ at web.de> wrote:

> Rafael Knuth wrote:
>
> > Thank you, I am using Python 3.3.0
>
> [Oscar]
> > In Python 3 you should use input(). In Python 2 you should use
> > raw_input(). I'm guessing that you're using Python 2. In Python 2 the
> > input() function tries to evaluate whatever the user types in as if it
> > was Python code. Since Rafael is not a defined variable it fails. The
> > fix is to use raw_input() which just returns a string.
>
>
> [Rafael]
> > I am not sure I understand.
> > "Rafael" is the user's in put, and that value is assigned to the variable
> > "name".
> > I made sure only a string is accepted as input
> >
> > name = (str(input("What's your name?"))
> >
> > Can you clarify? Thank you in advance.
>
> As Oscar says you are invoking your script with Python 2. Python 2's
> input()
> function evals user input as a Python expression. For example if you run a
> script
>
> print input("your input please: ")
>
> and you type
>
> 1 + 1
>
> the script will print
>
> 2
>
> Likewise if you type
>
> Rafael
>
> the script will look up the value of a variable named Rafael. This doesn't
> exist and therefore you get an exception.
>
> But this is all a distraction -- how exactly are you invoking what you
> think
> is Python 3.3.0? What is your operating system?
>
> If you are using Linux or OSX open a terminal window and try to run your
> script from that terminal window with
>
> python3.3 ~/Documents/3_Tufcik.py
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130520/9e39ce5c/attachment-0001.html>


More information about the Tutor mailing list