0

I could use some help figuring out why my game will not allow me to pick up a sword. The error messege that comes up is

Traceback (most recent call last): File "", line 36, in File "", line 1, in NameError: name 'y' is not defined

Here is the basic game, I am very new to this, so simple terminology would be nice.

print 'You enter a Dungeon with three doors. Do you want to enter door #1 door #2 or door #3?'
door = raw_input('> ')

if door == "1":
    print 'Theres a dragon eating a human, the dragon is massive and looks terrifying'
    print 'what do you want to do?'
    print '#1 try to save the human'
    print '#2 scream and run around'
    dragon = raw_input('> ')

    if dragon == "1":
        print 'You approach the dragon sneakily. After what feel like a thousand years you are finally close to the dragon. You try to step closer. The dragon shifts and crushes you. Well done!'

    elif dragon == "2":
        print 'You scream and run around like a chicken with no head. The dragon is not impressed with you. It snorts and engulfs you in flames. You die a painful and worthless death. Well done!'

elif door == "2":
    print 'You stare into a deep dark cave.'
    print 'Oh dear, it seems you have been driven you quite insane.'
    print '#1. drool'
    print '#2. scream, drool, and walk forward'
    print '#3. Understand computer programming completely and get an A plus'
    insanity = raw_input('> ')

    if insanity == "1":
        print 'Your body survives but your mind does not, you drool for eternity!'

    if insanity == "2":
        print 'You slip on a mysterious liquid, then fall into a deep sleep, you awake years later at the kiss of a prince. Yay!'

    if insanity == "3":
        print 'Congradulations! You passed Programming! Time to graduate and face the real world.. So I guess you actully lost. Sorry.'

elif door == "3":
    print ("You emerge into a brighter area and see a small sword lying on the ground")
    ch1 = str(input("Do you take it? [y/n]:"))
    if ch1 == "y":
        print 'You have taken the sword!'
        stick = 1

else:
    print 'You have chosen not to take the sword'
    sword = 0
Bic
  • 3,141
  • 18
  • 29
  • This question has been asked [many](http://stackoverflow.com/questions/17413502/nameerror-name-n-is-not-defined-even-though-its-an-input-python), [many](http://stackoverflow.com/questions/2612948/error-in-python-d-not-defined) times. It's a duplicate of [this](http://stackoverflow.com/questions/2090706/basic-hello-world-in-python-isnt-working) – LondonRob Sep 09 '14 at 15:59

2 Answers2

6
ch1 = str(input("Do you take it? [y/n]:"))

If you use input and the user types "y", the interpreter will search for the variable y and return its value. You don't have a y variable, so it crashes. You probably meant to use raw_input here, not input.

ch1 = str(raw_input("Do you take it? [y/n]:"))

By the way, raw_input returns a string anyway, so you don't really need the str call.

ch1 = raw_input("Do you take it? [y/n]:")
Kevin
  • 74,910
  • 12
  • 133
  • 166
1

Answer Corrected:

use raw_input instead of input

In [32]: ch1 = raw_input("Do you take it? [y/n]:")
Do you take it? [y/n]:y

In [33]: ch1
Out[33]: 'y'
Sencer H.
  • 1,201
  • 1
  • 13
  • 35