After answering to a raw_input, nothing happens - python

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:

Related

If returning wrong answer (python)

I am working through the "Learn python the hardway." I apologize if this is a duplicate but I really can't figure out what I'm doing wrong. When I enter anything less than 50 it tells me to try again, and upon the second time calls a string in another function. If I enter something greater than 50 same thing on the second entry it calls another string in another function. it will call from green_dragon() and print "Both dragons cook you alive and eat you." Thank you for any insight you are able to offer. I apologize for the simplicity, lol. Had to make my own "game and I'm not that creative yet, lol.
def gold_room():
print "You have entered a large room filled with gold."
print "How many pieces of this gold are you going to try and take?"
choice = raw_input("> ")
how_much = int(choice)
too_much = False
while True:
if how_much <= 50 and too_much:
print "Nice! you're not too greedy!"
print "Enjoy the gold you lucky S.O.B!"
exit("Bye!")
elif how_much > 50 and not too_much:
print "You greedy MFKR!"
print "Angered by your greed,"
print "the dragons roar and scare you into taking less."
else:
print "Try again!"
return how_much
def green_dragon():
print "You approach the green dragon."
print "It looks at you warily."
print "What do you do?"
wrong_ans = False
while True:
choice = raw_input("> ")
if choice == "yell at dragon" and wrong_ans:
dead("The Green Dragon incinerates you with it's fire breath!")
elif choice == "approach slowly" and not wrong_ans:
print "It shows you its chained collar."
elif choice == "remove collar"and not wrong_ans:
print "The dragon thanks you by showing you into a new room."
gold_room()
else:
print "Both dragons cook you alive and eat you."
exit()
too_much = False
if <= 50 and too_much
if too_much is set to False, why do you expect the if expression to evaluate to true ? It will never go inside the if.
Move user input inside loop as well.
EDIT:
To stop your while loop:
too_much = True
while too_much:
choice = raw_input("> ")
how_much = int(choice)
if how_much <= 50:
print "Nice! you're not too greedy!"
print "Enjoy the gold you lucky S.O.B!"
too_much = False
else:
print "You greedy MFKR!"
print "Angered by your greed,"
print "the dragons roar and scare you into taking less."

I am having a syntax error [closed]

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."

How do I move to different parts of code in this game/story

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?

How to allow a maxium of 3 else loops, after which it should run another function?

I'm pretty new to programming and I'm currently going through "Learn Python The Hard Way". I'm doing exercise 36, which asked to create my own simple text based game.
Unfortunately I've ran into a problem. What I'm trying to do, is basically make the function allow a maximum of 3 times of repeated use of 'else', after which it should run 'dead()' function.
I understand that using 'while', it will loop 'else' forever. I tried creating a 'for'-loop which should build a list and after number 3 it should run 'dead()', but that didn't really work. I tried positioning it before and after 'while', but that failed as well.
def genie():
print "Suddenly a genie appears!"
print "The genie says that it will grant 3 of your wishes. "
print "Do you want to wish something, or leave him?"
while True:
next = raw_input("> ")
if next == "wish":
print "You want your 3 wishes granted. "
wishes()
elif next == "leave":
print "The genie gets angry at you for wasting its time! "
print "You suddenly appear in a huge room with a dragon in it. "
dragon_room()
else:
print "The genie doesn't understand your muttering."
i = 0
angry = []
for i in angry:
print "The genie has got a little bit annoyed."
i = i + 1
angry.append(i)
if i == 3:
dead("The genie got angry and killed you!")
else:
exit()
I think what you want is something like:
for i in range(3):
next = raw_input("> ")
if next == "wish":
...
wishes()
break
elif next == "leave":
...
dragon_room()
break
print "The genie doesn't understand your muttering."
if i:
print "The genie has got a little bit annoyed."
else:
dead("The genie got angry and killed you.")
exit()
The else on a for (or while) loop only runs if the loop doesn't break early.
You should place a count variable outside the while loop, and increase it by 1 when "The genie not understand".
print "Suddenly a genie appears!"
print "The genie says that it will grant 3 of your wishes. "
print "Do you want to wish something, or leave him?"
count = 0
while True:
next = raw_input("> ")
if next == "wish":
print "You want your 3 wishes granted. "
wishes()
elif next == "leave":
print "The genie gets angry at you for wasting its time! "
print "You suddenly appear in a huge room with a dragon in it. "
dragon_room()
else:
print "The genie doesn't understand your muttering."
if count == 3:
dead("The genie got angry and killed you!")
else:
print "The genie has got a little bit annoyed."
count += 1

Conditionals aren't working in my Python code

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":

Categories