1

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("Well, " + myName + ", I'm thinking of a number between 1 and 20.")

while guessTaken < 6:
   print("Take a guess.")
   guess = input()
   guess = int(guess)

   guessTaken = guessTaken + 1

   if guess < number:
       print("You guessed a little bit too low.")

   if guess > number:
       print("You guessed a little too high.")

   if guess == number:
       break

if guess == number:
    guessTaken = str(guessTaken)
    print("Well done " + myName + "! You guessed the number in " + guessTaken + " guesses!")

if guess != number:
    number = str(number)
    print("No dice kid. I was thinking of this number: " + number)

This is the error I get:

Name error: Name 's' is not defined.

I think the problem may be that I have Python 3 installed, but the program is being interpreted by Python 2.6. I'm using Linux Mint if that can help you guys help me.

Using Geany as the IDE and pressing F5 to test it. It may be loading 2.6 by default, but I don't really know. :(

Edit: Error 1 is:

File "GuessingGame.py", line 8, in <Module>
myName = input()

Error 2 is:

File <string>, line 1, in <Module>
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
  • 1
    what line do you get the error? You probably just have some extraenous `s` floating around (maybe a leftover hidden at the end of a long line) – ChristopheD May 07 '10 at 17:55
  • I thought this wasn't enough for an answer, so I'll just comment it: I had bad experiences with Python 3 and code not written specifically for it, and I recommend you Python 2.6 if your want to learn it, because 95%* of the python code for learning online is made for Python 2.6 (* may not be true.) – LukeN May 07 '10 at 17:55
  • I'm following this tutorial: http://inventwithpython.com/chapters/ and it recommend following along with Python 3. O_O –  May 07 '10 at 17:58
  • @ChristopheD: I suspect the 's' was part of the input. In Python before 3.0, `input` attempts to evaluate the input as a Python expression. – Fred Larson May 07 '10 at 17:59

3 Answers3

5

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?

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • I installed Linux Mint 8 and it comes with Python 2.6. I manually installed Python 3 as well (sudo apt-get install python3) and also installed Geany as the IDE. Geany is automatically using 2.6 as it's interpreter and I want to use Python 3. If I knew the terminal command to launch a python script using 3 my problem would be solved. Any help? –  May 07 '10 at 18:02
  • 2
    The terminal command would be `python3 GuessingGame.py` – Thomas Wouters May 07 '10 at 18:03
  • Yep that worked like a charm and my initial assesment was spot on. Geany was using 2.6 instead of 3.0 like I wanted. Thanks again guys! –  May 07 '10 at 18:07
2

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

Thomas Wouters
  • 130,178
  • 23
  • 148
  • 122
0

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.)

AlG
  • 14,697
  • 4
  • 41
  • 54
  • This sort of thing makes me wonder when Python 3 will hit some kind of "tipping point" when almost-everybody-uses it. I am not a big fan of the "functionification" of the print statement, but this input() glitch is something I really didn't expect to stay as it was, from Python 1.0 until now. – Warren P May 07 '10 at 18:15