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()
Related
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
I'm new to this website so please bear with my questioning problems on the website. I need some help to finish off this mini text-based game as homeowork of learning python the hard way book. This is the code I wrote. and I don't know the missing links and what I've done wrong. Help would be appreciated!
from sys import exit
def start():
print "You are in an old temple."
print "There is a door to your right and left or you can walk forwad."
print "Which one do you take?"
choice = raw_input("> ")
if choice == "left":
gold_room()
elif choice == "right":
trap_room()
elif choice == "forward":
monster_room()
else:
dead("you got caught by the ancient gods and you must be killed.")
start()
def monster_room():
print "you're in a room with a monster. what you gonna do?"
choice = raw_input("> ")
if "left" in choice:
print "you are going to the gold room"
gold_room()
elif "right" in choice:
print "you are going to the trap room"
trap_room()
else:
dead("couldnt understand what did you say so you are dead!")
def gold_room():
print "you chose the left room. now you are in a room with a pot of gold!"
print "you can take the pot."
print "or you can just rob the money in it."
print "or you go go to other rooms."
choice = raw_input("> ")
if choice == "take the pot":
print "you are a millionaire from now on!!!"
elif choice == "rob the money":
dead("you will never rest in piece!")
else choice == "another room":
monster_room()
def trap_room():
print "you are now in a trap room."
print "there is a hidden trap in this room."
print "be careful!"
print "you can go back to the monster room"
print "or you can find the trap"
choice = raw_input("> ")
if "find" in choice:
start()
elif "back" in choice:
gold_room()
def dead(why):
print why, "rekt!"
exit(0)
Ok, I've fixed it. Your code has several indentation mistakes, Python requires four spaces or one tab indentation after a def statement.
Another thing is, that you used else with a condition test (else choice == "another room":). That is wrong, it should be elif choice == "another room": or just else.
You also may have noticed, that I changed raw_input() to input(). This converts all inputs to strings (input() will interpret intergers as integers, lists as lists and so on...), and is also more secure.
The last thing is, you run your program (start()) before definign all called functions, this cannot work!
Your code (fixed):
from sys import exit
def start():
print("You are in an old temple.")
print("There is a door to your right and left or you can walk forwad.")
print("Which one do you take?")
choice = input("> ")
if choice == "left":
gold_room()
elif choice == "right":
trap_room()
elif choice == "forward":
monster_room()
else:
dead("you got caught by the ancient gods and you must be killed.")
def monster_room():
print("you're in a room with a monster. what you gonna do?")
choice = input("> ")
if "left" in choice:
print("you are going to the gold room")
gold_room()
elif "right" in choice:
print("you are going to the trap room")
trap_room()
else:
dead("couldn't understand what did you say so you are dead!")
def gold_room():
print("you chose the left room. now you are in a room with a pot of gold!")
print("you can take the pot.")
print("or you can just rob the money in it.")
print("or you go go to other rooms.")
choice = input("> ")
if choice == "take the pot":
print("you are a millionaire from now on!!!")
elif choice == "rob the money":
dead("you will never rest in piece!")
elif choice == "another room":
monster_room()
def trap_room():
print("you are now in a trap room.")
print("there is a hidden trap in this room.")
print("be careful!")
print("you can go back to the monster room")
print("or you can find the trap")
choice = input("> ")
if "find" in choice:
start()
elif "back" in choice:
gold_room()
def dead(why):
print(why, "rekt!")
exit(0)
start()
I would call the start() function at the end, after all the functions you defined. A usual way to write that is to write the following code at the end:
if __name__ == "__main__":
start()
This basically means that the programm runs the start() function at the end, if you're executing the file.
Furthermore you have to leave spaces after defining a function. You wrote:
def monster_room():
print "you're in a room with a monster. what you gonna do?"
but it should be:
def monster_room():
print "you're in a room with a monster. what you gonna do?"
If that didnt help, specify the problem
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.
I'm learning to code through learn python the hard way, and I've recently gotten stuck for the first time. For this exercise we're supposed to write our own game. I did so, but for some reason whenever I run it the right_room() function exits after I put in an answer, instead of proceeding to the next room. Any help would be greatly appreciated. Here's my code:
from sys import exit
def bear_room():
print "You are in a room with a bear."
print "You have two choices. left or right?"
next = raw_input("> ")
if next == "left":
left_room()
elif next == "right":
right_room()
else:
print "No idea what that means..."
def left_room():
print "You went left."
print "There are two doors. right or straight"
next = raw_input("> ")
if next == "right":
bear_room()
elif next == "straight":
second_left()
else:
print "What are you saying, bro?"
def second_left():
print "You went straight."
print "You again have two choices. straight or right?"
next = raw_input("> ")
if next == "straight":
print "You won! Congrats."
exit(0)
elif next == "right":
dead("You opened the door and walked off a cliff. Goodbye!")
else:
print "I didn't quite catch that."
def right_room():
print "You went right."
print "There are two doors. straight or right?"
next == raw_input("> ")
if next == "right":
dead("Oops, a tiger just ate you")
elif next == "straight":
second_right()
else:
"What?!?!?!"
def second_right():
print "You went straight"
print "Nice choice."
print "You have two choices: left or straight"
next == raw_input("> ")
if next == "left":
dead("You just fell 1 million feet to your death.")
elif next == "straight":
print "You made it out alive!"
exit(0)
else:
"WTF?"
def dead(reason):
print reason, "good job!"
exit(0)
def start():
print "You are about to enter a room."
bear_room()
start()
It looks like you're trying to assign to the next variable, but you used the equality check operator (==).
from sys import exit
haskey = 0
# start function
def start():
print "You wake up in an empty room, feels like you've been here for days. You can't remember anything from your past. All there is in the room is a digital clock. It says 3:26am, May 5, 2012. Get out of the room?"
next = raw_input("> ").lower()
if "yes" in next:
lobby()
elif "no" in next:
print "We insist"
else:
print "Try again."
def lobby():
while True:
print "You arrived at a lobby, all you can see are four doors. Which door to enter? (first, second, third, fourth)?"
next = raw_input("> ").lower()
if "first" in next:
firstdoor()
elif "second" in next:
seconddoor()
elif "third" in next:
thirddoor()
elif "fourth" in next:
fourthdoor()
else:
print "Are you dumb and you can't even follow instructions?"
def firstdoor():
print "You arrive at another empty room, examine further or exit?"
choice = raw_input("> ").lower()
if "examine" in choice:
print "A trap door opened, you fell in it and died."
exit()
elif "exit" in choice:
lobby()
else:
print "Are you dumb and you can't even follow instructions?"
def seconddoor():
print "You arrive at the study room, examine room or exit?"
choice = raw_input("> ").lower()
if "examine" in choice:
print "There is a note on the table, read it?"
secondchoice = raw_input("> ").lower()
if "yes" in secondchoice:
note()
elif "no" in secondchoice:
print "Returning to lobby."
lobby()
def note():
print """Security Log (040412): A man from the city travelling along the highway loses control of his vehicle and fell to the cliff. He was able to jump and grab a hold to the bushes growing at the side of the cliff. We were able to rescue him, but as soon as we secured him to the ground he violently reacted to our help and fainted. A few minutes later he was out of control, like he was possessed by a demon. We had no choice but to sedate him and keep him locked in our prison until authorities from the city arrive and examine him. The key to his cell is in the vault in the vault room. The keycode changes depending on the current date.
"""
print "Returning to lobby."
lobby()
def thirddoor():
if haskey == 0:
print "Door is locked, you need a key to continue."
print "%d" % haskey
lobby()
elif haskey == 1:
exit()
def exit():
print "You are now free!"
print "To be continued.."
def fourthdoor():
print "There is a vault inside the room. Use vault?"
usevault = raw_input("> ")
if "yes" in usevault:
vault()
else:
print "Returning to lobby.."
lobby()
def vault():
while True:
print "There is a security code for this door. Enter code:"
code = raw_input("> ")
if "05042012" in code:
print "Correct!"
print "Returning to lobby.."
haskey = int(1)
print "%d" % haskey
lobby()
else:
print "Code Error! Try again?"
start()
I have this mini-text game for a tutorial on python and I'm using the fourthdoor/vault function to ask the player the code and if entered correctly it changes the value of a variable to be used as a key to open the third door. The problem is even if the value of the variable is changed when the vault code is given correctly, I still can't open the door.
Can anyone help me?
When python encounters haskey = int(1) inside of vault, it creates a new local variable called haskey that you can only see inside of vault. You need to tell python that when it sees haskey in that function, you mean the global haskey that you declare at the top of the file. You can do this by adding global haskey to the beginning of vault. ie:
def vault():
global haskey
while True:
print "There is a security code for this door. Enter code:"
code = raw_input("> ")
...