I'm currently going through Learn Python The Hard Way Exercises and wanted to try writing my own code to let a user choose different paths. When I run the code below, I don't get a name error for "first_decision". However, I keep getting a name error saying it's not defined for the variable "second_decision". I am running Python 2.7.10. Does anyone know why this is?
Error Here:
NameError: name 'second_decision' is not defined
Code Here:
print "This is the root level where we now create 3 branches of decisions. 1, 2, or anything else"
first_decision = raw_input("> ")
print "You chose %r" % first_decision
if first_decision == "1":
print "This is the context after player makes the first choice"
print "Once here, we can let the player make another decision. 1, 2, or 3"
second_decison = raw_input("> ")
print "You chose %r" % second_decision
if second_decision == "1":
print "This is two levels deep"
elif second_decision == "2":
print "This is two levels deep"
else:
print "Everything else for the second level"
elif first_decision == "2":
print "This is the second context after player makes the first choice"
print "Once here, we can let the player make another decision. 1, 2, or 3"
second_decison = raw_input("> ")
print "You chose %r" % second_decision
if second_decision == "1":
print "This is two levels deep"
elif second_decision == "2":
print "This is two levels deep"
else:
print "Everything else for the second level"
else:
print "This is for everything else"
You have wrong name, you assing to "second_decison" and then use "second_decision"
you missed i.
You made a typo in the line
second_decison = raw_input("> ")
This should be
second_decision = raw_input("> ")
Related
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
This is the code I have written for Learn Python the Hard Way exercise 36. But I am not able to run the code in the door_1 function. If I choose 3 as an option and then left or right anything which is then stored in dir, the output is "lion ate you", no matter what I type.
from sys import exit #importing module from lib
def Tadaa():
print "This is a place where you will get a surprise"
next = int(raw_input("Enter any number from 1-10 :"))
if next <= 10:
if next % 2 == 0 :
print "You will be buying me a shake :)."
else :
print "You will be getting a shake by me."
else :
print "Do it correctly."
def door_1():
print "There are 3 doors . Choose any door from the the remaining three doors"
print "Lets hope for the best "
next = raw_input("Enter your option :")
if next == "1":
print "abc "
elif next == "2":
print "abc"
elif next == "3":
print "You have entered 3rd door ."
print "Here are 2 doors one on left and one on right."
dir = raw_input("Choose which door do you wnat to enter :")
if dir == "left" or "Left":
print "Lion ate you . You are dead ."
elif dir == "right" or "Right" :
print "You will be getting a surprise"
Tadaa()
else :
print "abc"
else :
print "abc"
def door_2():
print "There are two doors A and B which will decide your fate"
next = raw_input("Enter the door ")
if next == "A" or "a":
door_1()
elif next == "B" or "b":
print "You are back from where you have started"
start()
else :
print "I got no idea what that means."
exit(0)
def start():
print "You are in dark room"
print "There is a door to your right and left ."
print "Which one do you take"
next = raw_input("> ")
if next == "Right" or "right":
door_2()
elif next == "Left" or "left":
door_1()
else :
print "abc"
start()
The problem is your statement:
if dir=="left" or "Left":
What you want is
if dir=="left" or dir=="Left":
In effect, what just doing or "Left" is doing, is checking whether a string that you've just created exists or not. Put another way, its similar to:
x='Left'
if x:
X does exist, so if X is True.
The crux here is to always evaluate statements in quantum, and when you're using and in conjunction with or statements, make sure you use brackets to be explicit. if statement_one or statement_two.
In python, I am trying to make this code accept the user to move forward if he writes "True", and not if he writes "False" for the statement in User_Answer. When I run the code however, I get the "the answer is correct!"-part no matter what I write. The part I am having trouble with starts with "Test_Answer".
Could anyone help me with this?
name_list = ["Dean", "Bill", "John"]
enter_club = ["Enter", "enter"]
print ("THE CLUB - by Mads")
print (" ")
print ("""You approach a secret club called \"The club\". The club members are dangerous.
Make sure you tell the guard one of the members names.""")
print ("")
print ("Good evening. Before you are allowed to enter, we need to check if your name is on our list.")
def enter_the_club():
enter_now = input(" \nPress \"enter\" to enter the club... ")
if (enter_now in enter_club) == True:
print (" ")
print ("But as you enter, you are met with an intelegence test. \n It reads:")
check_name = input("What is your name? ")
def list_check():
if (check_name in name_list) == True:
print("Let me check.. Yes, here you are. Enjoy yourself, %s!" % check_name)
enter_the_club()
elif check_name.isalpha() == False:
print("Haha, nice try %s! Let's hear your real name." % check_name)
list_check()
elif (check_name in name_list) == None:
print ("You will need to give us your name if you want to come in.")
list_check()
else:
print ("I am sorry, but I can not find your name on the list, %s." % check_name)
print ("Are you sure that's your listed name?")
list_check()
list_check()
print ("But as you enter, you are met with an intelegence test.")
print (" ")
print ("It reads:")
Test_Answer = True
def IQtest():
User_Answer = input("Is 18/4 % 3 < 18 True or False? ")
if Test_Answer == User_Answer:
print ("Great, %s, the answer is correct!" % check_name)
else:
print ("You are not allowed to enter before the answer is correct, %s!" % check_name)
IQtest()
IQtest()
True is a boolean constant. What the user enters will be either "True" or "False", both character strings. Also, your elif condition cannot be true. What are you trying to do with three decision branches?
Without changing your code too much ... try this?
Test_Answer = "True"
def IQtest():
User_Answer = input("Is 18/4 % 3 < 18 True or False? ")
if Test_Answer == User_Answer:
print ("Great, %s, the answer is correct!" % check_name)
else:
print ("You are not allowed to enter before the answer is correct, %s!" % check_name)
IQtest()
Note that I've also corrected your "else" syntax.
Also, you have no value for check_name; I assume this is a global variable that you've handled elsewhere.
In this code I try to call a function. On the other hand its not working the way I want it to work, for it is staying in the loop even though I am changing the loops condition options_secondscenario[0] from 0 to 1 at the end of the Loop. What I want this to do is to move to third_scenario() function. Thanks in advance.
options_secondscenario = ['Whats going on out there?', 'So what now?']
def third_scenario():
print "This is the third scenario"
choiceselection2 = raw_input("> ")
if choiceselection2 == 1:
print "stay"
else:
print "leave"
def dead():
print "You are dead"
exit()
def second_scenario():
print "Conversation 1"
print "Conversation 2"
print "Conversation 3"
print options_secondscenario
choices = options_secondscenario[0]
while choices == options_secondscenario[0]:
choiceselection = raw_input("> ")
if choiceselection == 'Whats going on out there?':
print "Conversation 4"
elif choiceselection == 'Whats up':
print "Nothing"
elif choiceselection == 'what is that':
print "I dont know"
elif choiceselection == 'is':
dead()
else:
third_scenario()
choices == options_secondscenario[1]
second_scenario()
You are trying to change your choices var AFTER the loop (check your indentation). So the while loop never gets a chance to reach this statement.
Also, you are using the compare operator == instead of the assignment operator = like so:
choices = options_secondscenario[1]
That line should be somewhere INSIDE your while loop. Check your indentation.
I'm having trouble inputting scoring in my python quiz code. Here is the script:
#This is the Test script for )TUKT(, Developed by BOT.
print ""
print "Welcome to the Ultimate Kirby Test."
print ""
begin = (raw_input("Would you like to begin?:"))
if begin == "yes":
print ""
print "Alright then! Let's start, shall we?"
print "Q1. What color is Kirby?"
print "1) Black."
print "2) Blue."
print "3) Pink."
print "4) Technically, his color changes based on the opponent he swallows."
choice = input("Answer=")
if choice ==1:
print "Incorrect."
print ""
elif choice ==2:
print "Incorrect."
print ""
elif choice ==3:
print "Tee hee.... I fooled you!"
print ""
elif choice ==4:
score = score+1
print "Well done! You saw through my trick!"
print ""
elif choice > 3 or choice < 1:
print "That is not a valid answer."
print ""
print "Well done! You have finished my test quiz."
print("Score:")
print ""
#End of Script
The error always says
score = score+1 is not defined.
I did not get anywhere researching.
Thanks! Help is very much appreciated!
You forgot to define a variable called score. You can't reference a value that doesn't exist!
Just declare it at the start:
score = 0
In the line score = score + 1, Python goes: 'so I need to create a variable called score. It contains the value of score, plus 1.' But score doesn't exist yet, so an error is thrown.
The varibale score has never been defined. Insert score = 0 as first line of your scirpt.
How can I have Python move to the top of an if statement if no condition is satisfied correctly.
I have a basic if/else statement like this:
print "pick a number, 1 or 2"
a = int(raw_input("> ")
if a == 1:
print "this"
if a == 2:
print "that"
else:
print "you have made an invalid choice, try again."
What I want is to prompt the user to make another choice for this if statement without them having to restart the entire program, but am very new to Python and am having trouble finding the answer online anywhere.
A fairly common way to do this is to use a while True loop that will run indefinitely, with break statements to exit the loop when the input is valid:
print "pick a number, 1 or 2"
while True:
a = int(raw_input("> ")
if a == 1:
print "this"
break
if a == 2:
print "that"
break
print "you have made an invalid choice, try again."
There is also a nice way here to restrict the number of retries, for example:
print "pick a number, 1 or 2"
for retry in range(5):
a = int(raw_input("> ")
if a == 1:
print "this"
break
if a == 2:
print "that"
break
print "you have made an invalid choice, try again."
else:
print "you keep making invalid choices, exiting."
sys.exit(1)
You can use a recursive function
def chk_number(retry)
if retry==1
print "you have made an invalid choice, try again."
a=int(raw_input("> "))
if a == 1:
return "this"
if a == 2:
return "that"
else:
return chk_number(1)
print "Pick a number, 1 or 2"
print chk_number(0)
Use a while loop.
print "pick a number, 1 or 2"
a = None
while a not in (1, 2):
a = int(raw_input("> "))
if a == 1:
print "this"
if a == 2:
print "that"
else:
print "you have made an invalid choice, try again."