I am having a syntax error [closed] - python

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

Related

After answering to a raw_input, nothing happens

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:

Python programming, syntax error for game

#makes a varible called name to identify the players name
name=input("Hello person, Whats your name?")
#prints their name
print("Hello", name)
#console asks the player if they want to play the game, if choice is "yes" then continue, else say "ok bye bye" (at the bottom!!)
print("Do you want to hear a story?", name)
choice=input("Yes, No?")
if choice==("yes" or "yes " or "Yes" or "Yes "):
print("Ok", name,", listen up")
print("There was once an old, old house at the top of a hill Sooooo high it was above the clouds")
housename=input("What do you want to call the house?")
print("The old,",housename,"was once owned by an old lady. You decide to go up to the", housename, ",you encounter a ghost in your path. You see a varitety of weapons beside you, an axe, sword and a bow.")
#asks the player if they want an axe sword or bow
choice3=input("Do you choose the axe, sword or bow?")
#if the choice is "bow" then proceed with this code
if choice3==("bow" or "Bow" or "bow " or "Bow "):
print("You equip the shoddy Bow, The bow feels as if it could snap any second.")
#sets the enemyshealth as 10
enemyhealth=int(10)
#makes a while loop to keep the battle going instead of 1 time.
while enemyhealth >= 1:
print("Take a shot!")
bowattack=input("Type attack to fire an arrow!")
if bowattack==("attack"):
import random
#randomiser for damage generator
damage = ["1", "2", "3", "4"]
damage2 = int(random.choice(damage))
enemyhealth = enemyhealth - damage2
print("The ghost took some damage. Enemys health:", enemyhealth)
else:
print("Are you sure you typed shoot?")
#if the enemys health gets below 1 print you killed the ghost, reward system! **this is what im having trouble with!!**
if enemyhealth <= 1:
print("You killed the Ghost!!")
print("You vanquished the ghost, you now collect a new weapon!")
#confirms the reward, either gives the player a shiny bow or a double shot bow.
import random
reward = ["Shiny bow", "Doubleshot bow"]
#randomiser for either reward
reward2 =(random.choice(reward)
#prints what weapon the player got
#THIS IS THE PROBLEM, ON THIS LINE
print("You got a:", reward2)
#pointless easteregg :D
elif choice==("maybe"):
print("You found an easter egg, congrats. PS this does nothing")
#if the player typed anything other than yes say ok bye bye.
else:
print("Ok, bye bye", name)
I am fully aware that the code does not yet have the other 2 if statements for the axe or sword. What i'm having trouble with is the reward generator for killing the ghost. I think its an indention error; it says syntax error for the print line.
I know this is rather alot of code to take in but i'd really appreciate it if could help me fix this; if you see anything I could make a shortcut around that would also be helpful.
Im using python 3.4.2!
You're missing a closing parenthesis in the previous line:
reward2 =(random.choice(reward)
should be:
reward2 =(random.choice(reward))
Also, the line if choice==('yes' or 'yes ' or 'Yes' or 'Yes '): wont work.
Try this:
if choice.strip().lower() == 'yes':
# whatever
strip will remove whitespaces. lower will put the string in lowercase
The same happens with choice3

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

Invalid python syntax [closed]

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 )

Categories