Skip to content Skip to sidebar Skip to footer

Problem Running A Python Program, Error: Name 's' Is Not Defined

Here's my code: #This is a game to guess a random number. import random guessTaken = 0 print('Hello! What's your name kid') myName = input() number = random.randint(1,20) print

Solution 1:

When you enter data for input() in Python 2, you're entering a Python expression. Whatever you're typing

  1. Looks like an expression -- not a literal.

  2. Has an S in it (hence the undefined variable.)

Either

  • put your strings in quotes or

  • stop using input() and use raw_input()

  • stop using Python 2.6.

It's not clear what "I have Python 3 installed, but the program is being interpreted by Python 2.6." means. If it's installed, why isn't it being used? What's wrong with your PATH?

Solution 2:

If you want to run this in Python 2, you will have to replace the calls to input() with raw_input().

Solution 3:

Simple fix. Change your input to raw_input and off you go. For example:

myName = raw_input("Hello! What's your name kid? ")

Check out the Python documentation for more details, but you want to avoid using input as it's attempting to eval() what is returned;

Warning

This function is not safe from user errors! It expects a valid Python expression as input; if the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation. (On the other hand, sometimes this is exactly what you need when writing a quick script for expert use.)

Post a Comment for "Problem Running A Python Program, Error: Name 's' Is Not Defined"