Part of code not executing (no errors shown) - python

This is a chatbot, that includes a choose your own adventure game, however if you select the 2nd option for the first choice (explore the beach) it doesn't execute anything after that in the game.
When I select the first option, everything works fine, so I don't understand why it's not showing. The game itself works fine if I run it separately from the chatbot, and I've checked the indenting already. Please help.
import random
#Choose YOur Own Adventure Game ========================================
print("Would you like to play a Choose Your Own Adventure game?\n")
game1 = input("Play a Choose YOur Own Adventure Game? ")
if (game1 == "Yes" or game1 == "yes"):
print("Alright! Let's get started...")
countdown = 5
while (time > 0):
print (("...") + str(time) + ("..."))
time = time - 1
print("Loading Game... 3, 2, 1..\n")
print("Welcome player!")
print("Hello " + name + ", welcome to this adventure game!\n")
print("You find yourself in a strange hut, in a dazed state. \nWhere am I? you wonder.")
print("You see lots of light near the door, & when you walk out you see... ")
print("... wow, so much sand!\n")
drown = random.randint(1,2)
help = random.randint(1,2)
#============================ Story starts here ========================================================
print("Turns out you're on a beach,\nYou don't know how you got here.")
print("The messy hut isn't too far from the shore.")
print("You walk onto the white sand, it's hot,\nand it seems like there's no one around.\n")
print("Weird, so you decide to look around.")
print("Maybe you can find some info on how you got here.\n")
#===================================Choice 1 =====================================
print("Do you want to examine the hut for clues or explore the beach?")
print("1: Examine the hut\n2: Explore the beach\n")
explore = int(input("Examine the hut or explore the beach?"))
while (explore < 1 or explore > 2):
print("Please enter 1 or 2.\n")
explore = int(input("Examine the hut or explore the beach?"))
if (explore == 1):
print("You go back into the hut.")
print("Near the door you notice something glistening.")
print("You walk over and decide to take a closer look.")
#========================================= Choice 2 ====================================
print("You find a small clear vial with some mysterious liquid in it.")
print("I should:\n1: Drink it.\n2: Just put in my pocket.\n")
drink =int(input("Drink it or save for it later?"))
while (drink < 1 or drink > 2):
print("Please enter either 1 or 2.\n")
drink =int(input("Drink it or save for it later?"))
#=========================================== Choice 3 =========================================
if (drink == 2):
print("You put it in your pocket.\n")
print("There's nothing else in the hut.")
print("You then go back onto the beach.")
print("You go for a walk near the water.")
print("You don't find anything interesting.")
print("You decide to go for a swim.")
print("The water envelopes you, and you drown.")
print("Game Over.")
print("This is one of the 8 endings thanks for playing!")
if (drink == 1):
print("You drink the whole bottle, it made you feel weird.")
print("All of a sudden you feel drowsy.")
print("Do you want to take a nap to feel refreshed?")
print("1: Yes\n2: No\n")
nap = int(input("Want to take a nap?"))
while (nap < 1 or nap > 2):
print("Enter 1 or 2")
nap = int(input("Do you want to take a nap to feel refreshed?"))
if(nap == 1):
print("You decide to doze off...\n")
naptime =int(input("How long you want to sleep for?(in hours)"))
if (naptime < 3):
print("You took a short nap.\n When you woke up you decided to go relax on the beach.")
print("The water looks cold, just what you need on a hot day like this.")
print("You decide to go for a swim.\n")
print("Some time later...\n")
if (drown == 1):
print("While in the water,a strong current swept you away from the shore, \nyou're not a great swimmer, so you drowned.")
print("This is one of the 8 endings, thanks for playing!")
if (drown == 2):
print("Exhausted from swimming, you lay down on the sand to rest.")
print("You were never good with puzzles or survival,\nit doesn't seem like theres a way to get off this island.\n")
print("Do you want to give up on the thought of escaping?")
print("1: Yes\n2: No")
give_up = int(input("Give up on escaping?"))
while (give_up < 1 or give_up > 3):
print("Enter 1 or 2")
if (give_up == 1):
print("You gave up on escaping the island.")
print("Without food or water you slowly waste away.")
print("You died, not knowing where you are or how you got there.\n")
print("This is one of the 8 endings, thanks for playing!")
if (give_up == 2):
print("You decide to keep looking for an exit.")
print("After a few days it feels like you've searched every nook and cranny.")
print("You don't find anything.")
print("Game Over.")
print("This is one of the 8 endings, thanks for playing!")
#========================================== Ending A ================================================
if (naptime > 3):
print("'Hey! Get up! You've been voted off'\nyou hear a man's voice shout")
print("(a few minutes later)\nApparently this is one of those game shows, y'know like Survivor")
print("Watching me sleep for so long was too boring, so I have been kicked off.")
print("This is one of the 8 endings, thanks for playing!")
if(nap == 2):
print("Everything became hazy, as you stagger onto the hot sand.")
print("Everything goes black as you fall straight into the water.\n")
print(name + " wake up! Are you alright?")
print("Huh? Oh... it's just an interesting dream.")
print("Whew, what a weird nightmare... you sigh.")
print("This is one of the 8 endings, thanks for playing!")
#============================================ If you explore the beach =========================================
if (explore == 2):
print("You decide to go for a walk near the water.")
print("You don't find anything out of the ordinary, it's just a beach.")
print("Do you want to relax on the beach?")
print("1: Yes\n2: No\n")
relax = int(input("Relax?"))
while (relax < 1 or relax > 2):
print("Enter 1 or 2")
relax = int(input("Relax?"))
if (relax == 1):
print("You laid down on the sand.")
print("You fall asleep.")
print("Thump, thump, .... THUD\n")
print("You open your eyes,everything is shaking.")
print("It's, it's... an earthquake!")
print("The raggedy hut collapses, any clues that might've been there are destroyed.\n")
print("You go examine the remains of the hut.")
print("Within the sand you find a flare gun!")
print("You use it, and a ship picks you up.")
print("You've escaped!")
print("This is one of the 8 endings, thanks for playing!")
if (relax == 2):
print("With nothing else to do you go back into the hut.\n")
print("Upon further examination you see a small vial full of some liquid.")
print("The liquid is clear and odorless.")
print("I should:\n1: Drink it.\n2: Just put in my pocket.\n")
drink =int(input("Drink it or save for it later?"))
if(drink == 1):
print("You're not sure what you just drank, but one thing's for sure,")
print("you are probably going to faint.")
print("Everything goes black...\n")
print("Hey! Wake up!")
print("You wake up to see two lifeguards standing over you.")
print("You passed out from dehydration, and everything was just a hallucination.")
print("This is one of the 8 endings, thanks for playing!")
if(drink == 2):
print("You put it in your pocket.")
print("You are no closer to escaping than before.\n")
print("All of a sudden you catch a glimpse of ship passing by!")
print("Maybe if they hear you you can catch a ride off this island!\n")
print("Do you want to call out to them?")
print("1: Yes\n2: No\n")
scream = int(input("Try asking for help?"))
if(scream == 1):
if(help == 1):
print("Your screams were successful!")
print("You are saved!")
print("This is one of the 8 endings, thanks for playing!")
if(help == 2):
print("Whoever was on the ship didn't seem to hear you.")
print("It seems like there's nothing you can do.")
print("Game Over.")
print("This is one of the 8 endings, thanks for playing!")
if(scream == 2):
print("Nobody comes to your rescue.")
print("You are trapped, and without any food or water you slowly perish.")
print("Game Over.")
print("This is one of the 8 endings, thanks for playing!")
while (scream < 1 or scream > 2):
print("Enter 1or 2.\n")
scream =int(input("Try asking for help?"))
while (drink < 1 or drink > 2):
print("Please enter either 1 or 2.\n")
drink =int(input("Drink it or save for it later?"))
#End of Game ===============================================================
elif (game1 == "No" or "no"):
print("Oh, I see you don't want to play...")
print("How sad, I spent all that time setting it up for you ;(")
print("Oh well, I guess we won't play.\n")

The problem with your code is you have put the statement if explore == 2 inside the if explore == 1. Python gives a lot of importance to indentation. All you need to do is indent the second if statement with the first one. In your code.
Sample:
x=2
if x==1:
if x==2:
body....
This is the sample of your statement. Here, x=2. First it checks if x==1. If no then goes to other statement. But you have defined if x==2 inside the first one. So it will be executed only if the first statement is True.
So the correct expression is:
x=2
if x==1:
body....
elif x==2:
body....
This is the case in your code.
if (explore == 1):
print("You go back into the hut.")
print("Near the door you notice something glistening.")
print("You walk over and decide to take a closer look.")
#========================================= Choice 2 ====================================
print("You find a small clear vial with some mysterious liquid in it.")
print("I should:\n1: Drink it.\n2: Just put in my pocket.\n")
drink =int(input("Drink it or save for it later?"))
while (drink < 1 or drink > 2):
print("Please enter either 1 or 2.\n")
drink =int(input("Drink it or save for it later?"))
#=========================================== Choice 3 =========================================
if (drink == 2):
print("You put it in your pocket.\n")
print("There's nothing else in the hut.")
print("You then go back onto the beach.")
print("You go for a walk near the water.")
print("You don't find anything interesting.")
print("You decide to go for a swim.")
print("The water envelopes you, and you drown.")
print("Game Over.")
print("This is one of the 8 endings thanks for playing!")
if (drink == 1):
print("You drink the whole bottle, it made you feel weird.")
print("All of a sudden you feel drowsy.")
print("Do you want to take a nap to feel refreshed?")
print("1: Yes\n2: No\n")
nap = int(input("Want to take a nap?"))
while (nap < 1 or nap > 2):
print("Enter 1 or 2")
nap = int(input("Do you want to take a nap to feel refreshed?"))
if(nap == 1):
print("You decide to doze off...\n")
naptime =int(input("How long you want to sleep for?(in hours)"))
if (naptime < 3):
print("You took a short nap.\n When you woke up you decided to go relax on the beach.")
print("The water looks cold, just what you need on a hot day like this.")
print("You decide to go for a swim.\n")
print("Some time later...\n")
if (drown == 1):
print("While in the water,a strong current swept you away from the shore, \nyou're not a great swimmer, so you drowned.")
print("This is one of the 8 endings, thanks for playing!")
if (drown == 2):
print("Exhausted from swimming, you lay down on the sand to rest.")
print("You were never good with puzzles or survival,\nit doesn't seem like theres a way to get off this island.\n")
print("Do you want to give up on the thought of escaping?")
print("1: Yes\n2: No")
give_up = int(input("Give up on escaping?"))
while (give_up < 1 or give_up > 3):
print("Enter 1 or 2")
if (give_up == 1):
print("You gave up on escaping the island.")
print("Without food or water you slowly waste away.")
print("You died, not knowing where you are or how you got there.\n")
print("This is one of the 8 endings, thanks for playing!")
if (give_up == 2):
print("You decide to keep looking for an exit.")
print("After a few days it feels like you've searched every nook and cranny.")
print("You don't find anything.")
print("Game Over.")
print("This is one of the 8 endings, thanks for playing!")
#========================================== Ending A ================================================
if (naptime > 3):
print("'Hey! Get up! You've been voted off'\nyou hear a man's voice shout")
print("(a few minutes later)\nApparently this is one of those game shows, y'know like Survivor")
print("Watching me sleep for so long was too boring, so I have been kicked off.")
print("This is one of the 8 endings, thanks for playing!")
if(nap == 2):
print("Everything became hazy, as you stagger onto the hot sand.")
print("Everything goes black as you fall straight into the water.\n")
print(name + " wake up! Are you alright?")
print("Huh? Oh... it's just an interesting dream.")
print("Whew, what a weird nightmare... you sigh.")
print("This is one of the 8 endings, thanks for playing!")
#============================================ If you explore the beach =========================================
elif (explore == 2):
print("You decide to go for a walk near the water.")
print("You don't find anything out of the ordinary, it's just a beach.")
print("Do you want to relax on the beach?")
print("1: Yes\n2: No\n")
relax = int(input("Relax?"))
while (relax < 1 or relax > 2):
print("Enter 1 or 2")
relax = int(input("Relax?"))
if (relax == 1):
print("You laid down on the sand.")
print("You fall asleep.")
print("Thump, thump, .... THUD\n")
print("You open your eyes,everything is shaking.")
print("It's, it's... an earthquake!")
print("The raggedy hut collapses, any clues that might've been there are destroyed.\n")
print("You go examine the remains of the hut.")
print("Within the sand you find a flare gun!")
print("You use it, and a ship picks you up.")
print("You've escaped!")
print("This is one of the 8 endings, thanks for playing!")
if (relax == 2):
print("With nothing else to do you go back into the hut.\n")
print("Upon further examination you see a small vial full of some liquid.")
print("The liquid is clear and odorless.")
print("I should:\n1: Drink it.\n2: Just put in my pocket.\n")
drink =int(input("Drink it or save for it later?"))
if(drink == 1):
print("You're not sure what you just drank, but one thing's for sure,")
print("you are probably going to faint.")
print("Everything goes black...\n")
print("Hey! Wake up!")
print("You wake up to see two lifeguards standing over you.")
print("You passed out from dehydration, and everything was just a hallucination.")
print("This is one of the 8 endings, thanks for playing!")
if(drink == 2):
print("You put it in your pocket.")
print("You are no closer to escaping than before.\n")
print("All of a sudden you catch a glimpse of ship passing by!")
print("Maybe if they hear you you can catch a ride off this island!\n")
print("Do you want to call out to them?")
print("1: Yes\n2: No\n")
scream = int(input("Try asking for help?"))
if(scream == 1):
if(help == 1):
print("Your screams were successful!")
print("You are saved!")
print("This is one of the 8 endings, thanks for playing!")
if(help == 2):
print("Whoever was on the ship didn't seem to hear you.")
print("It seems like there's nothing you can do.")
print("Game Over.")
print("This is one of the 8 endings, thanks for playing!")
if(scream == 2):
print("Nobody comes to your rescue.")
print("You are trapped, and without any food or water you slowly perish.")
print("Game Over.")
print("This is one of the 8 endings, thanks for playing!")
while (scream < 1 or scream > 2):
print("Enter 1or 2.\n")
scream =int(input("Try asking for help?"))
while (drink < 1 or drink > 2):
print("Please enter either 1 or 2.\n")
drink =int(input("Drink it or save for it later?"))

Related

How to make the program go back to the top of the code with a while loop?

I need to include a decrement life counter that has 5 lives. I need to use a while loop and once the player loses a life it needs to send them back to the choice between going left and right in the code. I am new to python so I am not very familiar with it, any help is appreciated.
answer = input("Do you want to go on an adventure? (Yes/No) ")
if answer.lower().strip() == "yes":
x=5
while x > 0:
print("You have ",x,"lives left.")
if x > 0:
break
x-=1
if x == 0:
break
answer= input("You are lost in the forest and the path splits. Do you go left or right? (Left/Right) ").lower().strip()
if answer == "left":
answer = input("An evil witch tries to cast a spell on you, do you run or attack? (Run/Attack) ").lower().strip()
if answer == "attack":
print("She turned you into a green one-legged chicken, you lost!")
elif answer == "run":
print("Wise choice, you made it away safely.")
answer = input("You see a car and a plane. Which would you like to take? (Car/Plane) ").lower().strip()
if answer == "plane":
print("Unfortunately, there is no pilot. You are stuck!")
elif answer == "car":
print("You found your way home. Congrats, you won!")
elif answer != "plane" or answer != "car":
print("You spent too much time deciding...")
else:
print("You are frozen and can't talk for 100 years...")
elif answer == "right":
import random
num = random.randint(1, 3)
answer = input("Pick a number from 1 to 3: ")
if answer == str(num):
print("I'm also thinking about {} ".format(num))
print("You woke up from this dream.")
elif answer != num:
print("You fall into deep sand and get swallowed up. You lost!")
else:
print("You can't run away...")
else:
print("That's too bad!")
You should try to add comments to your code, it will help you and others as well.
I think for getting user answer, you should use a different variable. It will make things easier.
I removed the following code, it didn't make any sense to me.
while x > 0:
print("You have ",x,"lives left.")
if x > 0:
break
x-=1
if x == 0:
break
--
This is the code, which works:
# Try to write imported modules at the top, it's a good practice.
import random
# Start the game
answer_start = input("Do you want to go on an adventure? (Yes/No) ")
# user chooses to play
if answer_start.lower().strip() == "yes":
lives=5
# function for displaying the lives
def display_lives():
print("You have ",lives,"lives")
#display lives
display_lives()
# As long lives are more than 0, this will keep going.
while lives>0:
#First choice
answer1= input("You are lost in the forest and the path splits. Do you go left or right? (Left/Right) ").lower().strip()
#User chooses left
if answer1 == "left":
answer2 = input("An evil witch tries to cast a spell on you, do you run or attack? (Run/Attack) ").lower().strip()
if answer2 == "attack":
print("She turned you into a green one-legged chicken, you lost!")
lives=lives-1
display_lives()
# User is running away
elif answer2 == "run":
print("Wise choice, you made it away safely.")
answer3 = input("You see a car and a plane. Which would you like to take? (Car/Plane) ").lower().strip()
if answer3 == "plane":
print("Unfortunately, there is no pilot. You are stuck!")
elif answer3 == "car":
print("You found your way home. Congrats, you won!")
elif answer3 != "plane" or answer3 != "car":
print("You spent too much time deciding...")
else:
print("You are frozen and can't talk for 100 years...")
elif answer1 == "right":
num = random.randint(1, 3)
answer4 = input("Pick a number from 1 to 3: ")
if answer4 == str(num):
print("I'm also thinking about {} ".format(num))
print("You woke up from this dream.")
elif answer4 != num:
print("You fall into deep sand and get swallowed up. You lost!")
lives=lives-1
else:
print("You can't run away...")
#Player chose not to play or player out of lives.
else:
print("That's too bad!")

Python coding for online Game - Pizzeria chose story Game.

I am stayed in getting the right coding. If you happen to run this code, please correct what you see fit. I have searched and there continues to be bugs here and there. Here is the game coding. There may be some issues in the definition terms and I'm still learning the Python vocabulary to defined new items and am not finding right answers. Here is the code that you can run and try. Thank you:
import random
import time
def displayIntro():
print("You are in a city. In front of you,")
print("you see two restraunts. In one pizzeria, the service is friendly")
print("and will share thier free pizza with you. The other pizzeria")
print("is very unpredictable and will hire you to wash the dishes in the back quick.!")
print()
def choosePizzeria():
pizzeria=""
while((pizzeria != "1") and (pizzeria != "2")):
print("Which pizzeria will you go into? (1 or 2) ")
pizzeria = input()
return pizzeria
def checkPizzeria(level,(pizzeria != "1") or (pizzeria != "2")):
print("You approach the pizzeria and see people hustling in and out quickly...")
time.sleep(2)
print("It really crowded with people waiting in line, playing arcade games and just having a good time dancing...")
time.sleep(2)
print("A little manager with a suit steps in in front of you! He quickly slips his arm behind his back...")
print()
time.sleep(2)
friendlypizzeria = random.randint(1, 3)
overworkPizzeria = friendlypizzeria + 1
if overworkPizzeria > 3:
overworkPizzeria -= 3
if chosenPizzeria == str(friendlyPizzeria):
print("Hand you a slip of paper for two free pizzas!")
elif chosenPizzeria == str(overworkPizzeria):
print("He hands you a slip of paper telling you to watch the dishes in the back for a while since you stared at him too long!")
else:
level += 1
if level < 3:
print("You quickly hid behind some customes and head out to 3 more pizzerias")
else:
print("Congratulations you win two free pizzas and")
print("you get to go to the manager's weekend party!!!! ")
return level
playAgain = "yes"
while playAgain == "yes" or playAgain == "y":
displayIntro()
level = 0
pizzeriaNumber = choosePizzeria()
level = checkPizzeria(level,pizzeriaNumber)
if level == 1:
pizzeriaNumber = choosePizzeria()
level = checkPizzeria(level,pizzeriaNumber)
if level == 2:
pizzeriaNumber = choosePizzeria()
level = checkPizzeria(level,pizzeriaNumber)
print('Do you want to play again? (yes or no)')
playAgain = input()
Thank you.
Mostly Syntax errors, I would suggest reading some Python documentation. Especially in regards to Types, and Comparison Operators. But below will run;
import random
import time
def displayIntro():
print("You are in a city. In front of you,")
print("you see two restraunts. In one pizzeria, the service is friendly")
print("and will share thier free pizza with you. The other pizzeria")
print("is very unpredictable and will hire you to wash the dishes in the back quick.!")
print()
def choosePizzeria():
pizzeria=""
while((pizzeria != 1) and (pizzeria != 2)):
print("Which pizzeria will you go into? (1 or 2) ")
pizzeria = input()
return pizzeria
def checkPizzeria(level, pizzariaNumber):
print("You approach the pizzeria and see people hustling in and out quickly...")
time.sleep(2)
print("It really crowded with people waiting in line, playing arcade games and just having a good time dancing...")
time.sleep(2)
print("A little manager with a suit steps in in front of you! He quickly slips his arm behind his back...")
print()
time.sleep(2)
chosenPizzeria = pizzariaNumber
friendlypizzeria = random.randint(1, 3)
overworkPizzeria = friendlypizzeria + 1
if overworkPizzeria > 3:
overworkPizzeria -= 3
if chosenPizzeria == friendlypizzeria:
print("Hand you a slip of paper for two free pizzas!")
elif chosenPizzeria == overworkPizzeria:
print("He hands you a slip of paper telling you to watch the dishes in the back for a while since you stared at him too long!")
else:
level += 1
if level < 3:
print("You quickly hid behind some customes and head out to 3 more pizzerias")
else:
print("Congratulations you win two free pizzas and")
print("you get to go to the manager's weekend party!!!! ")
return level
playAgain = "yes"
while playAgain == "yes" or playAgain == "y":
displayIntro()
level = 0
pizzeriaNumber = choosePizzeria()
level = checkPizzeria(level,pizzeriaNumber)
if level == 1:
pizzeriaNumber = choosePizzeria()
level = checkPizzeria(level,pizzeriaNumber)
if level == 2:
pizzeriaNumber = choosePizzeria()
level = checkPizzeria(level,pizzeriaNumber)
print('Do you want to play again? (yes or no)')
playAgain = raw_input()

Simple choice game Python IDLE 3.4 How to proceed with different choices?

I'm very new to programming and was doing a simple choice game:
Answer = (input("You meet a bear, what do you do? A) Give the bear a hug B) Run away"))
if Answer == ("A)"):
print("The bear chopped your hand off!")
else:
print("Good choice, but the bear is running after you")
But how do I go on? Like add an option after having proceeded with a chopped of hand or running through the forest (2 choices at least for both previous outcomes)
Here is a start you can hopefully figure out how to expand on :)
def main():
print("Tristan Aljamaa's Simple Python Choice Game")
print("===========================================")
print("Instructions: Type A) or B) etc. whenever prompted\n")
game()
def game():
Answer = (input("You meet a bear, what do you do?\n A) Give the bear a hug\n B) Run away \nEnter A) or B):"))
if Answer == ("A)"):
print("The bear chopped your hand off!")
player_died()
else:
print("Good choice, but the bear is running after you")
player_ran()
def player_died():
print("You died, the bear eventually ate you...")
Answer = (input("Game Over!\n\nEnter N) for New Game:"))
if Answer == ("N)"):
main()
else:
print("Good Bye!")
def player_ran():
Answer = (input("You find an exit from the forest, what do you do\n A) Exit forest\n B) Run around in forest \nEnter A) or B):"))
if Answer == ("A)"):
print("You exited the forest")
player_crossedRoad()
else:
print("You (although insanly) chose to run around in the forest")
player_stillRunsinForest()
def player_crossedRoad():
print("You get the idea...")
main() #for testing on this online editor use the below line when calling the .py file on your computer
if __name__ == "__main__":main()
Try the game out here
You could create different functions/procedures for different cases. For example:
def choppedHand():
selection = input("The bear chopped your hand off! What do you do now? \n 1.Fight the bear with one hand.\n 2. Scream and search for help.\n 3. Cry and beg for mercy")
if selection == "1":
fight()
elif selection == "2":
scream()
else:
cry()
def run():
selection = input ("Good choice, but the bear is running after you. What do you do now? 1.Run through the trees. 2.Run in the plain")
#etc etc, same as top.
Answer = (input("You meet a bear, what do you do?\n 1.Give the bear a hug.\n 2.Run away."))
if Answer == ("1"):
choppedHand()
else:
run()
This is just an example, but using functions you can create different options for different cases and call a function in other parts of your code. For example you character can lose his arm in a different situation and in this case you just need to recall you function choppedHand().
I hope this was what you were looking for.
def invalid():
print("Answer not valid")
def game():
Answer = input("You meet a bear, what do you do?\nA) Give the bear a hug\nB) Run away\n>? ")
if Answer == ("A"):
print("The bear chopped your hand off!")
Answer2 = input("The bear is still around you, what will you do?\nA) Bleed out and accept your fate\nB) Try to fight back\n>? ")
if Answer2 == ("A"):
print("You bled out and died")
elif Answer2 == ("B"):
print("You attempt to fight back\nYou have failed, and died")
elif Answer == ("B"):
Answer3 = input("You find a tree and the bear is still running, what do you do?\nA) Climb the tree\nB) Hide behind the tree\n>? ")
if Answer3 == ("A"):
print("You went up the tree, and the bear went away!")
print("You walked home and went to bed")
elif Answer3 == ("B"):
Answer4 = input("You fell down a hill and slid into a village, what do you do?\nA) Trade with a villager\nB) Head home\n>? ")
if Answer4 == ("A"):
Answer5 = input("There is only one merchant, will you trade?\nA) Yes\nB) No")
if Answer5 == ("A"):
print("You traded two gold coins for a sword made of strong steel")
print("You headed home")
elif Answer5 == ("B"):
print("You did not trade, so you headed home")
else:
invalid()
elif Answer4 == ("A"):
print("You headed home")
else:
invalid()
else:
invalid()
else:
invalid()
game()

Error: name "blue" not defined

When the user chooses option 4 on hall(), it should run blue(), but when I try to run it I get an error saying that blue() is not defined. How do I fix this?
import time
import sys
name = input ("Name: ")
print ("Hello", (name), ", and welcome to my game that i made to learn python.")
print ("How to play the game, just type the number its not that hard.")
time.sleep(5)
def intro():
print ("You are in a room, to the south there is a torch on the wall and to the north there is a door.")
time.sleep(5)
print ("Your options are: ")
time.sleep(3)
print ("1. Do nothing")
print ("2. Go south and pick up the torch")
print ("3. Go north, open and go through the door")
print ("4. You decide to build an orphanage in the room, makes sense.")
choice = input(">>> ")
if choice == "1":
print("You decide to curl into a ball and go to sleep, you never wake up again. --GAME OVER--")
print("I guess you could try again if you must.")
time.sleep(5)
intro()
elif choice == "2":
print("You walk southwards towards the wall, grab the torch off the wall and hold it in your hands.")
print("You walk over towards the door and open it")
time.sleep(5)
elif choice == "3":
print("You walk over towards the door and open it, on the other side there is a dark corridor, you step forward and the door closes behind you. You get surrounded in darkness and die. --GAME OVER--")
print("I guess you could try again if you must.")
time.sleep(5)
intro()
elif choice == "4":
print("You can't build an orphanage in a room with nothing there idiot, you wasted your whole life attempting. --GAME OVER--")
print("I guess you could try again if you must.")
time.sleep(5)
intro()
else:
print("Type the correct number idiot")
intro()
intro()
def hall():
print ("As you open the door a strong gust of cold air comes through making the torch flicker")
time.sleep(3)
print ("You continue up the corridor with the torch illuminating your surroundings, you feel like your being watched")
time.sleep(3)
print ("You keep walking for what seems like hours and you finally come across a part where the corridor splits off into 3 different ones")
print ("What do you do?")
print ("1. Go north.")
print ("2. Go east.")
print ("3. Go back the way you came.")
print ("4. Go west.")
time.sleep(5)
hall()
choice = input(">>> ")
if choice == "1":
print("You head north but as soon as you do the ground under you crumbles and you fall. And die. --GAME OVER--")
print("I guess you could try again if you must.")
time.sleep(5)
hall()
elif choice == "2":
print("You go down the east corridor and get a glimpse of a dark red light before it disappears.")
print("You continue to walk")
time.sleep(5)
red()
elif choice == "3":
print("Well done, you just went back to the place you wanted to get out from, your legs are tired and your torch has gone out. idiot. --GAME OVER--")
print("I guess you could try again if you must.")
time.sleep(5)
hall()
elif choice == "4":
print("You go down the west corridor and get a glimpse of a dark blue light before it disappears.")
print("You continue to walk")
time.sleep(5)
blue()
else:
print("Type the correct number idiot")
time.sleep(5)
hall()
def red1():
print ("As you continue to walk down the corridor the air around you seems to head up more and more.")
time.sleep(3)
print ("You come around a little podium and on it is a piece of paper with the numbers 264 894 written on it")
time.sleep(3)
print ("You go to pick it up but it crumbles into dust and under the dust are the words blue carved into the podium")
time.sleep(3)
print ("you continue walking")
time.sleep(3)
print ("After a while you come across a shiny iron door with a keypad by the side on the keypad it spells the words red on it.")
time.sleep(3)
print ("You attempt to enter the correct code.")
red1()
code1 = input(">>> ")
if code1 == "362 682":
print ("The door slides open without making a noise, you step into the door and continue walking")
else:
print ("Incorrect code. Hint:RED")
print ("restarting...")
time.sleep(10)
intro()
def blue1():
print ("As you continue to walk down the corridor the air around you seems to get colder and colder more and more.")
time.sleep(3)
print ("You come around a little podium and on it is a piece of paper with the numbers 362 682 written on it")
time.sleep(3)
print ("You go to pick it up but it crumbles into dust and under the dust are the words red carved into the podium")
time.sleep(3)
print ("you continue walking")
time.sleep(3)
print ("After a while you come across a rusty iron door with a keypad by the side on the keypad it spells the words red on it.")
time.sleep(3)
print ("You attempt to enter the correct code.")
blue1()
code2 = input(">>> ")
if code2 == "264 894":
print ("The door slides open without making a noise, you step into the door and continue walking")
else:
print ("Incorrect code. Hint:BLUE")
print ("restarting...")
time.sleep(10)
intro()
nice story btw). anyway to the problem.
you call function blue() that is not defined in your code - there is no such function ). if you meant to call blue1() you will get an error and that's because
you first need to declare the function before using it
for example:
1.this will not work:
blue()
def blue(): ...
2. this will work:
def blue(): ...
blue()
any way for good practice its good to maintain a simple code structure, all functions above and the main is the last one that calls other function.
The reason you're getting an error is because there indeed is no function called blue() defined. It's blue1() in your code. I strongly suggest investing in a linter.

How can I check to see what a user input in a different function?

This is my first post on stackoverflow and I'm only asking because I truly am stuck. A good friend and I are trying to make a simple text based game(let's hear it for the 80's). It's my first day essentially with python and I've run into a problem. The player has a choice to pick up a dagger or to not. Later on I want this decision to come into play. Except I've no idea how to check and see what said player entered!
Here's my code
Note:It crashes in the state it's in right now mainly because of me stumbling around trying to get this problem fixed.
def start():
print("You wake up in a dank tunnel reeking of death and decomposition,")
print("you have no weapons to defend yourself with.")
time.sleep(1)
print("\n")
print("You head north and find yourself at a four way tunnel.")
print("\n")
print("Down the west tunnel, you hear sounds which could have only come")
print("from hell itself. With every sound uttered, your fear grows.")
time.sleep(1)
print("\n")
print("Down the north tunnel, there is a room with a glimmering object")
print("lying on the ground.")
time.sleep(1)
print("\n")
print("Down the East tunnel, there appears to have been a cave in.")
time.sleep(1)
print("\n")
print("The South tunnel is the way you just came. \n")
time.sleep(1)
while True:
r3 = input("Which way would you like to go?\n")
if r3 == "west" or r3 == "West":
print("The sounds are becoming Louder, do you continue or head back?")
while True:
w1 = input("")
if w1 == "continue" or w1 == "Continue" or w1 == "west":
print("You continue further on...")
time.sleep(2.5)
westtunnel()
elif w1 == "head back" or w1 == "Head Back" or w1 == "back" or w1 == "Back":
print("\n")
print("The voices congregate behind you. No way your going back that way!\n")
time.sleep(1)
westtunnel()
else:
print("\n")
print("You couldn't possibly do anything else.\n")
time.sleep(1)
print("Greater minds prevail and you continue westward...\n")
time.sleep(2)
westtunnel()
elif r3 == "north" or r3 == "North":
print("You find a rusty dagger on the floor, stained with blood.")
print("Do you want to pick up the dagger?")
print("1: Yes, that seems useful!")
print("2: No, I'm a bit of a pacifist you see.")
while True:
pd = input("")
if pd == "1":
print("You slowly picked up the dagger.")
number = int(pd)
break
else:
print("You left the dagger. All alone.")
number = int(pd)
break
print("You can go only go back the way you came.\n")
time.sleep(1)
print("You head back to the four way tunnel.")
elif r3 == "east" or r3 == "East":
print("\n")
print("You can not go that way, there are rocks there. We told you this.\n")
time.sleep(1)
print("You go back to the four way tunnel")
elif r3 == "south" or r3 == "South":
print("\n")
print("This is the room you awoke in, there is nothing of interest.\n")
time.sleep(1)
print("You head back to the four way tunnel")
else:
print("\n")
print("You run into a corner, you hurt yourself in confusion. \n")
time.sleep(1)
print("You stumble back to the four way.")
def ladder():
print("Do you have a dagger?!")
number = pd
if number == "1":
print("Good you have it!")
start()
input("")
else:
print("awh...no dagger for you...")
start()
input("")
if __name__ == '__main__':
menu()
Look into Python classes.
You probably want to create a Game State object that holds the results of decisions made in the course of the game. Then, later, when the results of those decisions matter, you check the state.
You will need to keep a reference to that game state object during your main game loop. However, keeping it in one object keeps all the state information organized, instead of keeping references to several disparate variables.

Categories

Resources