Python Forum
Syntax error for a reason I don't understand
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax error for a reason I don't understand
#1
I receive a syntax error on the line if a == b: (line 46) I don't understand why. I believe that all the indentations are correct.

import random

ans = input("Welcome to escape the cave text adventure would you like to play? ")
if ans == "yes":
    inventory = []
    print ("")
    ans = input("You find yourself stuck in the middle of a cave with no obvious escape. You have two options, you go left or you go right: ")
    if ans == "left":
        print("You walk down a dark path and stumble into a pair of batteries, you pick them up. ")
        inventory.append("batteries")
        ans = input("You look up to see a dangling rope leading to an opening above you, you also see a path ahead of you which way do you go? (rope/path) ")
        if ans == "rope":
            print ("The rope snapped and fell, you died. GAME OVER")
        elif ans == "path":
            ans = input("You peer around the corner to see a old rickety bridge, do you want to cross it? ")
            if ans == "yes":
                ans = input("You cross the bridge safely and advance towards a glowing light, you get near and it is a fire, you must not be alone in the cave. Do you investigate the fire? ")
                if ans == "yes":
                    print("You feel a pair of hands on your back as you neel near the fire, you try to fight back but you can't and you are pushed into the fire. You die. GAME OVER")
                elif ans == "no":
                    ans = input("You realise that you can't be alone in the cave, you pannick and fall of a ledge injuring your leg. Do you give up and die or battle on? (give up/battle on) ")
                    if ans == "give up":
                        print ("You give up and die. GAME OVER")
                    elif ans == "battle on":
                        print ("You try but as you walk your leg gives way and you smash your head against a rock. You die. GAME OVER.")
            elif ans == "no":
                ans = input("You notice a hole in the ground and see it as the only other way to go. You drop down and fall into some dangerous rapids that drag you under. Do you sink or swim? ")
                if ans == "swim":
                    print("You try to swim but your struggle leads to you death. GAME OVER")
                elif ans == "sink":
                    print("You get sucked under and everything goes black. You wake up spluttering and on a ledge adjacent to the rapids. Lying there is a torch but with no batteries if you have batteries you can put them in.")
                    if ("batteries") in inventory:
                        inventory.remove("batteries")
                        inventory.append("torch")
                        ans == input("You shine your torch and see a bloody monster. Do you attack or run? ")
                        if ans == "run":
                            print("You tried to run but you tripped and fell leaving yourself defensless against the monster. GAME OVER")
                        elif ans == "attack":
                            ans = input("You attack and defeat the monster. You walk on, shining your torch, you see a glistening diamond sitting on a ledge. Do you take it? ")
                            if ans == "yes":
                                inventory.append("diamond")
                                ans = input("You walk along and on the wall you spot a machine asking you to guess a number between 1 and 5 for a reward. Do you play? ")
                                if ans == "yes":
                                    a = (random.randint(1,5))
                                    b = int(input("Enter a number between 1 and 5: ")
                                    if a == b:
                                        print ("that is the correct number, here is an emerald.")
                                        inventory.append("emerald")
                                        ans = input("You carry on walking and you bump into a shading looking man. He asks you if you have anything you could trade in exchange for your freedom. Do you want to trade? ")
                                        if ans == "yes":
                                            input("You can trade", inventory, "what do you choose? ")
                                            in ans in inventory:
                                            print("The man geatfully accepts your trade and shows you the way out the cave. CONGRATULATIONS, YOU WON!")
                                        else:
                                            print("That is the wrong number, the floor opens up beneath you. You die. GAME OVER")
                                    elif ans == "no":
                                        ans = input("You walk along and on the wall you spot a machine asking you to guess a number between 1 and 5 for a reward. Do you play? ")
                                            if ans == "yes":
                                            a = (random.randint(1,5))
                                            b = int(input("Enter a number between 1 and 5: ")
                                            if a == b:
                                                print ("that is the correct number, here is an emerald.")
                                                inventory.append("emerald")
                                                ans = input("You carry on walking and you bump into a shading looking man. He asks you if you have anything you could trade in exchange for your freedom. Do you want to trade? ")
                                                if ans == "yes":
                                                    input("You can trade", inventory, "what do you choose? ")
                                                    if ans in inventory:
                                                    print("The man geatfully accepts your trade and shows you the way out the cave. CONGRATULATIONS, YOU WON!")
                                                else:
                                                    print("That is the wrong number, the floor opens up beneath you. You die. GAME OVER")
                               

    
Reply
#2
 b = int(input("Enter a number between 1 and 5: ")
there is two opening brackets and only one closing
Reply
#3
as a side note - look at https://python-forum.io/Thread-Text-Adve...dictionary
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  World Clock syntax error OscarBoots 1 109 Yesterday, 05:20 AM
Last Post: snippsat
  coma separator is printed on a new line for some reason tester_V 4 532 Feb-02-2024, 06:06 PM
Last Post: tester_V
  Syntax error for "root = Tk()" dlwaddel 15 1,256 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 420 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,669 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,261 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,353 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,311 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 928 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Error I don't understand finndude 2 5,136 Oct-12-2022, 02:43 PM
Last Post: finndude

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020