Reputation: 57
I'm making a text based game off of Adventure Time. However, a value appears to not be saving. I'm on Windows 7 64-bit and I'm using Python 3.4.1
The error I get is
NameError: name 'character' is not defined
Please help me wih my problem. Thanks in advance.
def characterask ():
character = input ('''What character from the show should you be? (Finn or Jake)
If you are unfamiliar with the show, Finn is a human boy specialized in melee combat and Jake is a dog with the ability to stretch into anything.''')
try:
if character == "Finn" or character == "Jake":
print("Your character is ", character)
else:
print("That character's name is not Finn or Jake.")
print
characterask ()
except ValueError:
print ("That character's name is not Finn or Jake.")
print
characterask ()
print
characterask ()
time = input("What time is it?")
if (time == "Adventure Time"): print("Let's Go!") else: print("Nah,", name) print("It's Adventure Time!!!") print("Let's Go!")
print("You go to the Candy Kingdom. Ice King is freezing everything and is trying to kidnap Princess Bubblegum. He is flying with his magical beard powers.") def choice1 (): print("What will you do?!") if character == "Finn": answerq1c1 = input ('''a. Ask Jake to stretch you up so you can PUNCH HIM IN THE FACE!!!!! b. Go inside Princess Bubblegum's tower and climb up the stars.''') try: if answerq1c1 == "a": print("You climb on Jake's back.") print("Jake stretches his legs to be longer using his magical powers.") print("You jump off of Jake's back towards Ice King and punch him in the face.") print("You are now both falling from a life threatening height.") print("Jake stretches his arms and catches you.") print("Ice King uses his beard powers to regain flight just before he hits the ground.") print("Ice King: I'll get you! This isn't over!") print("Ice King idiotically flies into a wall.") print("Ice King: Ow!") print("Ice King: I'll get you!") else: print("Ice King notices you and Jake trying to get in the tower.") print("He freezes you both.") print("GAME OVER") print start () except ValueError: print("Your answer was not a or b.") print choice1 ()
else:
answerq1c2 = input ('''a. Stretch Finn up so he can punch Ice King IN THE FACE!!!
b. Go inside Princess Bubblegum's tower and climb up the stars.''')
try:
if answerq1c2 == "a":
print("Finn climbes on your back.")
print("You stretch your legs to be longer using your magical powers.")
print("Finn jumps off of Your back towards Ice King and Finn punches him in the face.")
print("They are now both falling from a life threatening height.")
print("You stretch your arms and catch Finn.")
print("Ice King uses his beard powers to regain flight just before he hits the ground.")
print("Ice King: I'll get you! This isn't over!")
print("Ice King idiotically flies into a wall.")
print("Ice King: Ow!")
print("Ice King: I'll get you!")
else:
print("Ice King notices you and Jake trying to get in the tower.")
print("He freezes you both.")
print("GAME OVER")
print
start ()
except ValueError:
print ("Your answer was not a or b.")
print
choice1 ()
print choice1 ()
Upvotes: 0
Views: 315
Reputation: 8937
character
cannot magically get into choice1
(in other words, character
exists in the scope of characterask
and is not defined in others). Specifically in the third line of it:
def choice1 ():
print("What will you do?!")
if character == "Finn":
# The rest of your function
To access it in choice1
you could:
characterask
)choice1
, orchoice1
.(Here's how you might do it, simplified down to just the code in question)
def characterask ():
character = input ('''What character from the show should you be? (Finn or Jake)
If you are unfamiliar with the show, Finn is a human boy specialized in melee combat and Jake is a dog with the ability to stretch into anything.''')
# Your validation stuff would go here...
return character
def choice1(character):
print("What will you do?!")
if character == "Finn":
# The rest of your function
usersCharacter = characterask()
choice1(usersCharacter)
Upvotes: 3
Reputation: 192
Your problem is that you aren't putting in a string. In the python interpreter, if you enter
c = input("Pick a letter or a number\n")
yes
you get your NameError. However if you do
c = input("Pick a letter or a number\n")
'yes'
you will correctly assign the variable. This is of course assuming that the problem is entirely within characterask, other posters have noted that this might be a problem of global vs. local variables. You might also consider other methods for collecting user input.
Upvotes: 0
Reputation: 238081
It seems that in function choice1()
you use character
variable in this line: if character == "Finn":
. However, this variable is a local variable in characterask()
function. Make character global or define it again in choice1.
Upvotes: 0
Reputation: 352
character
is local to characterask
. If you want to access it outside the function, you need to return the value.
Upvotes: 1