This is an attempt at a simple text adventure. I can't manage to make the conditionals work the way I want them to. When user input is given, such as 'left' when asked, it tells me 'left' isn't defined, even though I listed it in there. Here's the code.
from time import sleep
name = raw_input("What is your name? ")
live = raw_input("Where do you live? ")
print "%s. You have just come to your senses." % (name)
print "You are in %s, lying in the road." % (live)
sleep (5)
from datetime import datetime
now = datetime.now()
print "You look at your watch. There's a crack in the screen."
sleep (5)
print "Surely it cannot be %s/%s/%s at %s o clock..." % \
(now.day, now.month, now.year, now.hour)
sleep (5)
print "There is light blotting orange onto your closed eyes."
sleep (7)
print "You open your eyes."
sleep (5)
print "you see the road. Scarred, cracked and peppered with dead birds."
sleep (5)
print "Smoke thins out over the horizon."
sleep (5)
print "There is a house you are familiar with, to your right."
sleep (3)
print "There is a road leading uphill to your left."
sleep (3)
print "Where do you turn to?"
answer = raw_input("Type left or right and press Enter.")
if answer == left:
print "The road winds uphill."
sleep (5)
print "Glass crunches underfoot."
sleep (5)
print "The trees are all bare."
sleep (5)
print "The leaves turned to dust."
print "They are ground into the cracks, between the paving stones."
sleep (5)
if answer == right:
print "The corridor turns sharply."
sleep (2)
print "You go deeper inside until you can't see anything"
sleep(5)
print "You hear a noise from the floor below."
sleep (5)
print "You can just make out a white doorframe under the staircase."
sleep (5)
answer2 = raw_input("Do you go into the basement? Type yes, or no.")
if answer2 == yes:
print "The stairs creak. You fumble along the wall for a lightswitch"
sleep (5)
print "Do you press it?"
if answer3 == yes:
print "Light floods the basement. It leaves you blinded for a few seconds..."
sleep (5)
print "Were you pushed or did you fall?"
sleep (2)
print "No one remembers that day, or any other. The End"
if answer2 == no:
print "You can't see a thing. You decide to get out back to the street."
sleep (5)
print "Night is falling. You are hopelessly lost."
sleep (20)
sys.exit
if answer == "left":
you need to compare strings with strings
if answer == left:
is trying to compare the variable answer with the variable left, which python is correctly telling you that there is no variable named left
of coarse another possibility would be to define a variable named left at the top
left = "left"
(Keep in mind the same applies for your other comparisons)
It looks like you have not defined the variables left, right, yes nor no. Since the input is a string, you should compare it against a string:
if answer == "left":
...
if answer == "right":
...
if answer == "yes":
...
if answer == "no":
...
Note: A string in Python is enclosed by single quotes 'some string' or double quotes "another string", or, as #JoranBeasley mentioned, triple quotes """some multi-line string""" .
answer = raw_input("Type left or right and press Enter.")
if answer == "left":
print "The road winds uphill."
sleep (5)
print "Glass crunches underfoot."
sleep (5)
print "The trees are all bare."
sleep (5)
print "The leaves turned to dust."
print "They are ground into the cracks, between the paving stones."
sleep (5)
if answer == "right":
print "The corridor turns sharply."
sleep (2)
print "You go deeper inside until you can't see anything"
sleep(5)
print "You hear a noise from the floor below."
sleep (5)
print "You can just make out a white doorframe under the staircase."
sleep (5)
answer2 = raw_input("Do you go into the basement? Type yes, or no.")
if answer2 == "yes":
print "The stairs creak. You fumble along the wall for a lightswitch"
sleep (5)
print "Do you press it?"
if answer3 == "yes":
print "Light floods the basement. It leaves you blinded for a few seconds..."
sleep (5)
print "Were you pushed or did you fall?"
sleep (2)
print "No one remembers that day, or any other. The End"
if answer2 == "no":
print "You can't see a thing. You decide to get out back to the street."
sleep (5)
print "Night is falling. You are hopelessly lost."
sleep (20)
sys.exit
you need the quotes because otherwise the compiler assumes it is a variable and you want the constant value "left", "yes", etc. not a variable called left, yes etc
Your issue will likely be resolved with this:
if answer == "left":
Related
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 5 years ago.
So I have a few questions about the code below.
from sys import exit
def gold_room():
print "This room is full of gold. How much do you take?"
next = raw_input("> ")
gold_greedy = "50"
if next > gold_greedy:
dead("You are too greedy to live, die.")
elif next < gold_greedy:
dead("You are fair and therefore you win.")
else:
dead("Man, you BARELY made it")
def bear_room():
print "There is a bear here."
print "The bear has a bunch of honey."
print "The fat bear is in front of another door."
print "How are you going to move the bear?"
bear_moved = False
while True:
next = raw_input("> ")
if next == "Take honey":
dead("The bear looks at you then slaps you.")
elif next == "taunt bear" and not bear_moved:
print "The bear has moved from the door. You can go through it now."
bear_moved = True
elif next == "Taunt Bear" and bear_moved:
dead("The bear gets pissed off and chews your legs off.")
elif next == "open door" and bear_moved:
gold_room()
else:
print "I got no idea what that means."
def cthulhu_room():
print "Here you see the great evil Cthulhu."
print "He, it, whatever stares at you and you go insane."
print "Do you flee for your life or eat your head?"
next = raw_input("> ")
if "flee" in next:
start()
elif "head" in next:
dead("Well that was tasty!")
else:
cthulhu_room()
def dead(why):
print why, "Good job!"
exit(0)
def start():
print "You are in a dark room."
print "There is a door to your right and left."
print "Which one do you take?"
next = raw_input("> ")
if next == "left":
bear_room()
elif next == "right":
cthulhu_room()
else:
dead("You stumble around the room until you starve.")
start()
When I get to the gold_room, why when I take any letters instead of numbers it gives me "You are too greedy to live, die. Good job!", shouldn't it give me an error message? or give me the "Man, you BARELY made it" message?
If the user types anything other than whole numbers, how can I prompt him to type a number?
If you type:
print type(next)
you can see the next variable is of type str. You have to convert it to an integer by using the int() function:
next = int(raw_input("> "))
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am having a syntax error at
if first2 == 1:
import time
name = raw_input("What is your name? ")
print "Hello, " + name
time.sleep(1.5)
print "Welcome to Kill the Dragon."
time.sleep(2)
print "In this game, you will choose an option, and if you make the right
choices, you will slay the dragon."
time.sleep(5)
print "You are walking through a forest on a cold, damp, windy night."
time.sleep(3)
print "You see a castle, and as you are looking for a shelter, you decide to try your luck there."
time.sleep(3)
print "You are greeted by a knight in shining armor. He gives you a questioning look. What do you say?"
time.sleep(1)
first = int(raw_input("1: Say you are looking for shelter from the storm " "\n2: Say you are lost and need food "))
time.sleep(2)
if first == 1:
print "Ok, " + name + ",we could all use some help from this bad storm outside. Please let me lead you to a good place."
time.sleep(4)
print "After a good dinner, you ask if there is anything you can do to help."
time.sleep(2)
print "Well... There is one thing you can do. A dragon located in Eucalyptus Cave has been causing many problems lately./nIf you kill the dragon, we will give you a large reward."
time.sleep(1)
first2 = int(raw_input("1. Tell the knight you will kill the dragon.\n2. Tell the knight you will not kill the dragon. ")
if first2 == 1:
print "Oh, good. If you had declined, we would have thrown you into the dungeons.
if first2 == 2:
print "You will not kill the dragon for us? Off to the dungeons it is!"
time.sleep(1.2)
print "SLAM!"
if first == 2:
print "Ugg, I hate filthy peasants! Maybe if you kill the dragon living in that cave over there, we will let you stay."
time.sleep(4)
second2 = int(raw_input("1: Insist on getting inside the castle" + "\n2: Ask the knight for armor, a weapon, and a steed"))
if second2 == 1:
print "The knight tells you to get lost, and that the deal is off."
if second2 == 2:
print "The knight gives you things, and you set out to slay the dragon."
time.sleep(3)
second3 = raw_input ("Once you arrive at the cave, you see two ways to go. Should you go right or left? ")
if second3 == "right":
print "You are greeted by the carcusses of many older knights who died trying to battle the dragon. \nYou wish you didn't see it, and turn back to go the other way."
second3 = "left"
if second3 == "left":
print "You are greeted by the sleeping, green, slimy, two-headed dragon. \n He is sleeping, but he smells you and wakes up. \nHe is about to stand up. \nWhat do you do? "
if first2 == 1:
print "Oh, good. If you had declined, we would have thrown you into the dungeons.
Add a quotation mark at the end
if first2 == 1:
print "Oh, good. If you had declined, we would have thrown you into the dungeons."
I am trying to make a project where you make choices to defeat a dragon. Here is what I have:
import time
print "Hello"
time.sleep(1.5)
print "Welcome to Kill the Dragon."
time.sleep(2)
print "In this game, you will choose an option, and if you make the right choices, you will slay the dragon."
time.sleep(5)
print "You are walking through a forest on a cold, damp, windy night."
time.sleep(3)
print "You see a castle, and as you are looking for a shelter, you decide to try your luck there."
time.sleep(5)
print "You are greeted by a knight in shining armor. He gives you a questioning look. What do you say?"
time.sleep(4)
first = raw_input("1: Say you are looking for shelter from the storm " "2: Say you are lost and need food ")
time.sleep(5)
if first == 1:
print "Ok, we could all use some help from this bad storm outside. Please let me lead you to a good place."
time.sleep(5)
print "After a good dinner, you ask if there is anything you can do to help."
time.sleep(2)
if first == 2:
print "Ugg, I hate filthy peasants! If you go kill the dragon for us, maybe we will let you stay."
time.sleep(4)
print "1: Insist on getting inside the castle"
print "2: Ask the knight for armor, a weapon, and a steed"
second2 = raw_input()
The problem is when I answer "1" or "2," the code just stops running and it doesn't go to first == 1 or first == 2
Please tell me why, I am very new at Python.
Your problem is that raw_input() takes input as a string. Try casting int() to it.
>>> x = raw_input("Enter: ")
Enter: 1
>>> x
"1"
>>> x = int(raw_input("Enter: "))
Enter: 1
>>> x
1
Thus, here is your edited code:
import time
print "Hello"
time.sleep(1.5)
print "Welcome to Kill the Dragon."
time.sleep(2)
print "In this game, you will choose an option, and if you make the right choices, you will slay the dragon."
time.sleep(5)
print "You are walking through a forest on a cold, damp, windy night."
time.sleep(3)
print "You see a castle, and as you are looking for a shelter, you decide to try your luck there."
time.sleep(5)
print "You are greeted by a knight in shining armor. He gives you a questioning look. What do you say?"
time.sleep(4)
first = int(raw_input("1: Say you are looking for shelter from the storm " "2: Say you are lost and need food "))
time.sleep(5)
if first == 1:
print "Ok, we could all use some help from this bad storm outside. Please let me lead you to a good place."
time.sleep(5)
print "After a good dinner, you ask if there is anything you can do to help."
time.sleep(2)
if first == 2:
print "Ugg, I hate filthy peasants! If you go kill the dragon for us, maybe we will let you stay."
time.sleep(4)
print "1: Insist on getting inside the castle"
print "2: Ask the knight for armor, a weapon, and a steed"
second2 = raw_input()
raw_input returns strings, not integers:
>>> first = raw_input("1: Say you are looking for shelter from the storm " "2: Say you are lost and need food ")
1: Say you are looking for shelter from the storm 2: Say you are lost and need food 2
>>> first
'2'
>>> first == 2
False
>>> first == '2'
True
Consequently, replace:
if first == 1:
with:
if first == '1':
or with:
if int(first) == 1:
After the initial question of how much do you take, it works fine. If you type 0 you die, 5 million it says nice take. After it says nice take, it exits the program and forgets the rest of the code.
How do I get python to load the next part of the code and run it.
from sys import exit
def bank_vault():
print "This room is full of money up to 5 million dollars. How much do you take?"
choice = raw_input("> ")
if "0" in choice:
dead("You are robbing a bank and took nothing... The cops shot you in the face.")
how_much = int(choice)
if how_much < 5000000:
print "Nice take, now you have to escape!"
escape_route()
def escape_route():
print "There are cops blocking the front door."
print "There are cops blocking the back door."
print "There is no way to get out of there."
print "You see a small opening on the ceiling."
print "Type ceiling to go through it."
print "Or type stay to try your luck."
escaped_cops = False
while True:
choice = raw_input("> ")
if choice == "stay":
dead("Your bank robbing friends left your stupid ass and the cops shot you in the face. Idiot move dipshit.")
elif choice == "ceiling" and not escaped_cops:
print "You escaped! Now wash that money and don't go to prison."
escaped_cops = True
def dead(why):
print why, ""
exit(0)
bank_vault()
Cool game. I love games and would like to try to improve your code. Code below works on Python 3. It should work on Python 2:
from sys import exit
try: input = raw_input # To make it work both in Python 2 and 3
except NameError: pass
def bank_vault():
print("This room is full of money up to 5 million dollars. How much do you take?")
how_much = int(input("> ")) # Why not eliminate choice variable here?
if how_much == 0:
dead("You are robbing a bank and took nothing... The cops shot you in the face.")
elif how_much < 5000000: # Changed to else condition
print("Nice take, now you have to escape!")
else: # Added a check
dead("There isn't that much!")
def escape_route():
print("There are cops blocking the front door.")
print("There are cops blocking the back door.")
print("There is no way to get out of there.")
print("You see a small opening on the ceiling.")
print("Type ceiling to go through it.")
print("Or type stay to try your luck.")
escaped_cops = False
while not escaped_cops:
choice = input("> ")
if choice == "stay":
dead("Your bank robbing friends left your stupid ass and the cops shot you in the face. Idiot move dipshit.")
elif choice == "ceiling":
print("You escaped! Now wash that money and don't go to prison.")
escaped_cops = True
def dead(why):
print(why)
exit(0)
bank_vault()
escape_route()
You have to call the function escape_route rather than exit.
Also, when checking for choice you call dead no matter what.
In reply to your comment:
You need to check if it's 0 then call dead, if it's not 0 don't bother with the else, go directly to the if how_much < 5000000 and call escape_route
You need to try and convert the input to an int if it isn't 0!
And what happens if they take more than 5 mil?
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
from sys import exit
def gold_room():
print "This room is full of gold. How much do you take?"
next = raw_input(">")
if "0" in next or "1" in next:
how_much = int(next)
else:
dead("Man, learn to type a number.")
if how_much < 50:
print "Nice, you're not greedy, you win!"
exit(0)
else:
dead("You greedy bastard!")
def bear_room():
print "There is a bear here."
print "The bear has a bunch of honey."
print "The fat bear is in front of another door."
print "How are you going to move the bear?"
bear_moved = False
while True:
next = raw_input("> ")
if next == "take honey":
dead("The bear looks at you then slaps your face off.")
elif next == "taunt bear" and not bear_moved:
print "The bear has moved from the door. You can go through it now."
bear_moved = True
elif next == "taunt bear" and bear_moved:
dead("The bear gets pissed of and chews your legs off.")
elif next == "open door" and bear_moved:
gold_room()
else:
print "I got no idea what that means."
def cthulhu_room():
print "Here you see the great evil Cthulhu."
print "He, it, whatever stares at you and you go insane."
print "Do you flee for your life or eat your head(flee/head)?"
next = raw_input("> ")
if next == "flee":
start()
elif "head" in next:
dead("Well that was tasty!")
else:
cthulhu_room()
def dead(why):
print why, "Good job!"
exit(0)
def dog_room():
print "you entered a room and you see a dog sleeping and the door behind you got locked by it self"
print "you see a sign that says this dog got a very good hearing sense, above a normals dog hearing sense."
print "and you see a spear beneath you."
print "and you can see that there is a bridge behind him."
print "what will you do now?try to go to the bridge, pick up the spear, try to sneak your way to the dog and hit him or attack the door."
print "(bridge/spear/sneak and hit/attack the door)"
spear = False
while True:
action = raw_input("Choose what you want to do")
if action == "bridge" and not spear:
death("the dog woke up rushed to you and ate you right after he ate your balls."
elif action == "sneak and attack" and not spear:
death("you sneaked your way to the dog, hit him, and the damage you made to him wasn't strong enough and he ate you right after he ate your ball.")
elif action == "spear" and not spear:
spear = True
print "you took the spear. what now?"
elif action == "spear" and spear
print "you already took the spear..."
elif action == "sneak and attack" and spear:
golden_room("you stabbed the dog and went across the safe bridge with no casualties and you managed to get to the golden room!!!!!!!!")
elif action == "attack the door":
print "you broke the wooden door, the dog woke up, rushed to you, you tried to escape but the dog was faster"
print "and ate you right after he ate your balls."
death()
else:
print "*face palm* come on learn how to type!"
def start():
print "You are in a dark room."
print "There is a door to your right and left."
print "Which one do you take(left/right/forward)?"
next = raw_input("> ")
if next == "left":
bear_room()
elif next == "right":
cthulhu_room()
elif next = "forward":
dog_room()
else:
dead("You stumble around the room until you starve.")
start()
now for the problem
when i run it in power shell:
>>>python ex35.py
what i get is:
File "ex35.py", line 77
elif action == "sneak and attack" and not spear:
^
SyntaxError: invalid syntax
HELP!!! i tried to figure it out for an hour , hour and 30 minutes.
ty
if you are having problems to find the line
it is located right beneath the if line
which located inside the while loop which located inside the dog_room()
function.
Missing close paren:
death("the dog woke up rushed to you and ate you right after he ate your balls."
Missing trailing colon:
elif action == "spear" and spear
death("the dog woke up rushed to you and ate you right after he ate your balls."
--------------------------------------------------------------------------------^
You're missing a closing bracket )