how to make the game replay after the user losse? - python

def your_advntur() :
afdfdf="ff"
start = input("do you want to play ?\n")
a = "-deep voice:"
while start != "yes" :
if start == "no":
quit()
else:
start = input("pleas answer with yes or no:\n")
name = input(a+" hello adventurer tell me your name.\n-adventurer: my name is ")
path1 = input(a + name+" you found your self in jungle facing a lake without any memory this
is your story"
"\nyou can \"cross it\" or \"walk around it\" or \"head back\" what do you want to do
?\n-"+name+":")
if path1 == "cross the lake" :
print("brave adventurer which face his problems directly \n"
"i like it but infurtchn you have been eaten by an aligator\n GAME OVER")
your_advntur()
while True:
response = input("wanna play again?")
if response == "yes":
your_advntur()
else:
break
t tried this but it didnt work i want the game to restart after the user typing yes and close after no and reask the if you want to replay q if the user typed anthor thing

Related

How do I fix my algorithm from messing up my input in my choose your own adventure game?

I'm working on a choose your own adventure text game on python and I added an algorithm that makes it so if you press the H key your hunger is shown and if you press T the time left is shown. But when I run my code the script runs but after the first input option is entered for your name, the breakfast yes/no input question gets printed again and again no matter what is entered. When i delete my algorithm, it runs fine and goes into the next step. I know I probably made a very stupid mistake but I am fairly new and I would greatly appreciate it if I could get some help. Thank you!
My code:
import keyboard
hunger = 100
timeLeft = 100
yes_no = ["yes", "no"]
breakfast = ["eggs", "cereal"]
name = input("What is your name?\n")
print("Hello, " + name + "! This is the BREAKFAST STORY.")
answer = ""
while answer not in yes_no:
answer = input("You wake up one morning and are very hunrgy, do you get breakfast? (yes/no?)")
if answer == "yes":
print("You enter the kitchen, your hunger gets worse every second.")
elif answer == "no":
print("You starve to death. The End!")
quit()
answer = ""
while answer not in breakfast:
answer = input("What do you eat? (eggs/cereal?) ")
if answer == "eggs":
print("Good job! You fed yourself, you stayed alive and you live a happy life")
quit()
elif answer == "cereal":
print("Sorry, the cereal was 10 years overdue, you die terribly!. Rest in peace,"+ name +".")
quit()
#algorithm
if(keyboard.is_pressed('t')):
timeLeftFunction()
elif(keyboard.is_pressed('h')):
hungerFunction()
def timeLeftFunction():
timeLeft -= 20
print("TIME LEFT:"+timeLeft+"%")
def hungerFunction():
hunger -= 30
print("HUNGER"+"%")

Function not defined error although it is

About 3 days ago I started learning Python as a hobby.
Today I wanted to try to program a text adventure in Python.
When I tried to test the code I had an error, but I don't know why. Can someone please help me?
The code:
name = 'bot'
def intro():
print("On day you wake up but you don't remember anything, except your name.\n"
"You are in a unknown room. Right in front of you is a unlocked Computer.\n"
"\nWhat do you do?\n"
"\n1) Go to the computer.\n"
"2) Try to escape.\n"
"3) ")
anwser = input(">>> ")
if anwser.lower() == "1" or "go to the computer":
return computer()
elif anwser.lower() == "2" or "try to escape":
return escape()
intro()
def computer():
print("You go to the computer and there is a folder called" +name)
print("\nWhat do you do:\n"
""
"\n1) Open the folder.\n"
"2) Delete the folder\n"
"3) Lock the computer and try to escape.")
anwser = input(">>> ")
if anwser.lower() == "1" or "open the folder":
print("You open the folder an there a lot of dokuments.\n But you see a folder called \" Project Raspbrain \"")
elif anwser.lower() == "2" or "delete the folder":
print("You decide to delete the folder but you feel wired and fall on the ground.\n"
"You die!")
elif anwser.lower() == "3" or "lock the computer and try to escape":
escape()
def escape():
print("You see a door on your right side, you decide to go through it but there are two guards in front of you\n"
"What do you do?\n"
"\n1) Kill the guards"
"\n 2) Run")
anwser = input(">>>")
if anwser == "Kill the guards" or "1":
print("You try to kill the guards but don't have any weapons.\n Instead they kill you.\n You die")
elif anwser == "2" or "run":
print("You try to run but you stumble an fall on your head.\n You die!")
You need to move intro() to the end of this script, and your first line name = bot is very wired, I assume bot is a string and the code will look like below. The code will run well.
Remember that python is script language, it will run by order of your code, so in your scenario, you run intro() before the definition of computer(), and it will pop out not define error.
name = 'bot'
def intro():
print("On day you wake up but you don't remember anything, except your name.\n"
"You are in a unknown room. Right in front of you is a unlocked Computer.\n"
"\nWhat do you do?\n"
"\n1) Go to the computer.\n"
"2) Try to escape.\n"
"3) ")
anwser = input(">>> ")
if anwser.lower() == "1" or "go to the computer":
return computer()
elif anwser.lower() == "2" or "try to escape":
return escape()
def computer():
print("You go to the computer and there is a folder called" +name)
print("\nWhat do you do:\n"
""
"\n1) Open the folder.\n"
"2) Delete the folder\n"
"3) Lock the computer and try to escape.")
anwser = input(">>> ")
if anwser.lower() == "1" or "open the folder":
print("You open the folder an there a lot of dokuments.\n But you see a folder called \" Project Raspbrain \"")
elif anwser.lower() == "2" or "delete the folder":
print("You decide to delete the folder but you feel wired and fall on the ground.\n"
"You die!")
elif anwser.lower() == "3" or "lock the computer and try to escape":
escape()
def escape():
print("You see a door on your right side, you decide to go through it but there are two guards in front of you\n"
"What do you do?\n"
"\n1) Kill the guards"
"\n 2) Run")
anwser = input(">>>")
if anwser == "Kill the guards" or "1":
print("You try to kill the guards but don't have any weapons.\n Instead they kill you.\n You die")
elif anwser == "2" or "run":
print("You try to run but you stumble an fall on your head.\n You die!")
intro()
Please clarify what is "bot"?. Is it a pre-defined variable or just a syntax error?
For the function error just move the intro function call to the bottom
def intro():
(…)
def computer():
(…)
def escape():
(…)
intro()

I'm making a text based adventuere game in python and trying to add a third option but it's not working

I'm trying to make a simple text adventure game with three choices. But I can't seem to figure out why this isn't working.
This is the code I have been working on:
#Text based medieval adventure game
#Imported libraries are required
import random
import time
def displayWelcome():
print ("\nText adventure game\n")
print ("Welcome to the medieval adventure game!")
print ("You will have many choices through out this game")
print ("Be sure to pick the right one")
print (" Good Luck! ")
answer = askYesNo("Would you like help with this program? (Y/N): ")
if answer == "Y":
helpForUser()
time.sleep(3)
def askYesNo (question):
# This function will ask you a yes or no question and will keep asking until one is chosen.
# The following function is used to erase the variable response of any values
response = None
while response not in ("Y", "N"):
response = input (question).upper()
return response
def helpForUser():
#This function will show the user some helpful advice if they desire it
print ("\n+++++++++++++++++++++++++++++++++++++++ Help +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print ("This game is a adventure text based game set in the medieval times")
print ("You will be asked multiple questions with a yes or no response")
print ("you must answer the questions with the answers supplied to you suches as yes or no")
print ("If you don't answer the q uestion with the given answers it will be repeated untill a valid response occurs")
print ("The program can end when you choose")
print ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
def displayIntro():
#Displays intro to game
print ("\n It's been a rough day in the wild and you despratly need shelter")
print ("There is currently a war going on and nowhere is safe")
print ("But you intend to find somwhere with food and a bed but it wont be easy...")
print ()
def choosePath(lower,middle,upper):
#This functions allows you to choose between multiple options
path = 0
while path < lower or path > upper:
number = input("What path will you take(" + str(lower) + " - " + str(upper) + ")?: ")
if number.isdigit():
path = int (number)
else:
path = 0
return path
def followPath(chosenPath):
print ("you head down a long road\n")
time.sleep(3)
print ("You come across an abandoned campsite and decide to stay there for the night")
time.sleep(3)
print("You wake up to a sudden sound of voices and begin to realise that this campsite wasn't abandoned...")
time.sleep(3)
print("You start to freak out")
time.sleep(3)
if chosenPath == 1:
print("You grab your sword out and decide to go out the tent")
print ("Four well trained knights surround you")
print ("They strike without any hesitation, you counter two knights trying to hit you from the front as two from behind stab you in the back")
print ("The knights decide to burn your body and leave nothing left of you.\n")
elif chosenPath == 2:
print("You dart out of the tent and head for the exit")
print("All the guards try and get you whilst shooting arrows")
print("Several arrows hit you leaving you injured unable to run")
print ("Suddenly a man with a giant axe appears before you and slices you head off in a clean swoop\n")
else chosenPath == 3:
print("You keep calm and decide to sneak past all the guards")
print ("You're close to the exit when suddenly a guard notices you")
print("He's about to call for back up when you dash right into his chest with your sword, leaving it in him and running out the campsite")
print("You make it to a safe forest and decide to rest\n")
displayWelcome()
playAgain = "Y"
while playAgain == "Y":
displayIntro()
choice = choosePath()
followPath(choice)
playAgain = askYesNo ("Would you like to play again?")
print ("\nBye")
Error Number one: line 63, else should be elif or get rid of the condition "chosenPath == 3:
Current state
else chosenPath == 3:
What it should look like
elif chosenPath == 3:
or
else:
The other error is that nothing ever happens on initialization of choice because you forgot to input the "lower, middle, upper" values.

How do I get this choose your own adventure story in python to start over if the user enters that they want to play again?

Here is my code so far, which works:
print('Welcome to choose your own adenture.')
print('You enter a room with two doors. Do you want to enter the red door or the white door?')
door = input()
while True:
if door == 'white':
print("there was a dragon behind the door. It ate you and you died.")
break
elif door == 'red':
run_or_dead = input("there is a bear behind the door. Do you want to wave your arms and shout, or play dead?")
while True:
if run_or_dead == 'wave':
print("Nice try, but the bear did't buy it. It ate you and you died.")
break
elif run_or_dead == 'play dead':
print("Nice try, but the bear did't buy it. It ate you and you died.")
break
else:
run_or_dead = input('Not a valid answer. Do you want to wave or play dead?')
break
else:
door = input('that is not a valid choice. Do you want to enter the red door or the white door?')
I can't figure out how to get the program to ask the user if they want to repeat the whole process and do so if they enter 'yes'
You could put the game into a function, and call it as long as the user enters a predefined value:
def game():
print('Welcome to choose your own adenture.')
print('You enter a room with two doors. Do you want to enter the red door or the white door?')
door = input()
if door == 'white':
print("there was a dragon behind the door. It ate you and you died.")
return 0
elif door == 'red':
run_or_dead = input(
"there is a bear behind the door. Do you want to wave your arms and shout, or play dead?")
while True:
if run_or_dead == 'wave':
print("Nice try, but the bear did't buy it. It ate you and you died.")
return 0
elif run_or_dead == 'play dead':
print("Nice try, but the bear did't buy it. It ate you and you died.")
return 0
else:
run_or_dead = input('Not a valid answer. Do you want to wave or play dead?')
else:
door = input('that is not a valid choice. Do you want to enter the red door or the white door?')
return 0
while input('Do you want to play a game? [y/n]') == 'y':
game()
Since it's a function, you can't use break anymore to stop the game, but just a return will do the same.
One simple way is to surround your while True with another loop, for example:
play_more = True
while play_more:
# your while True loop goes here
uinput = input('Type yes if you wanna play more')
play_more = uinput.lower() == 'yes'

Python Maze Game trouble

I'm trying to make a game where you go through a maze and try to escape from a voice, but everytime the player says the wrong answer to one of the questions it says "Game Over" but then carries on where it kept off, I've tried a lot of things and researched, but I can't seem to figure it out, I'm only a beginner
`
import time
import os
print ("Your adventure starts as a young boy, running away from home becuase you're a rebel")
time.sleep(2)
print ("You find the famous labyrinth, do you go in?")
time.sleep(2)
answer = input("Make your choice, Yes OR No")
time.sleep(2)
print ("The answer",answer ,"got you stuck in a hole")
time.sleep(2)
print ("But you find a secret passage")
answer = input("Do you go through the door, Yes or No?")
if answer == "No":
time.sleep(2)
print ("Game Over.")
elif answer == "Yes":
time.sleep(2)
print("You hear a strange voice")
time.sleep(2)
answer = input("What do you say to the Voice, Hello or Who are you?")
if answer == "Hello":
print ("Hello")
elif answer == "Who are you?":
print ("Im your worst nightmare")
time.sleep(2)
print("You try and escape the labyrinth and turn a large gate with a gnome on the over end")
answer = input("Do you open the gate, Yes Or No?")
if answer == "Yes":
time.sleep(3)
print ("Game Over, you get brutally killed by a gnome, good job")
os._exit(0)
elif answer == "No":
time.sleep(3)
print ("You go the other way and see a light at the end of the tunnel")
answer = input("You see your family outside crying and waiting for you, do you go with them?")
if answer == "Yes":
print("You have a nice ending and you're sorry you ran away")
print("You have been graded: ")
elif answer == "No":
print("God smites you for being stupid.")
os._exit(0)`
take this block, for example
print ("But you find a secret passage")
answer = input("Do you go through the door, Yes or No?")
if answer == "No":
time.sleep(2)
print ("Game Over.")
elif answer == "Yes":
time.sleep(2)
print("You hear a strange voice")
time.sleep(2)
# continuation
if the user enters "No" it will print "Game Over" - which I assume is correct. However, control flow in the program continues past the if/else block. What you need to do is exit the program using something like sys.exit() or make sure your control flow only has paths forward if it should i.e. wrapping what happens next in the truthy part of the if/else block
if answer == "No":
time.sleep(2)
print ("Game Over.")
elif answer == "Yes":
time.sleep(2)
print("You hear a strange voice")
time.sleep(2)
# put continuation here

Categories

Resources