Search code examples
pythonpython-3.xterminalsyntaxide

I came across what is most likely is an indentation error, but I can't figure it out


I'm programming a text adventure game in python, and while fixing some syntax. I got what I think is an indentation error, but I can't seem to be able to fix it. My expected results were for the opening part in the game to run, me enter the main game, and be able to test the inventory system. now my question may be silly, but I'm just starting in python. the specific error is:

File "main.py", line 56
    else:
    ^
SyntaxError: invalid syntax

and my code is:

import random
room = random.randrange(100,400)
door = 0
closets = 0
invintorySlots = 0
# Menu 
print("                                    --------DOORS--------")
answer = input("                                   < Press enter to continue >")
#intro
print("You enter the hotel, its storming outside")
print("The receptionist, a girl with black hair and brown eyes says hi to you.")
print("Her: Your reserved room is number",room) 
print("  ")
answer = input("(A): Thanks!                                                                                                     (B): Your cute                                                                                                                          (C): ...")
if answer == "a":
    print("You: Thanks!")
    print("Her: No problem!")
    
elif answer == "b":
    print("You: Your cute.")
    print("Her: Im not really into dating right now.. Thank you though!")
    
elif answer == "c":
    print("Her: Not much of a talker eh?")
    print("You walk to your room")
    print("You enter but its not your room. its another room, with another door.")
    #main game
    door += 1
    print("You enter door",door)
    if closets > 0:
        if random.randrange(1,2) == 2:
            print("The lights flicker")
    answer = input("(A): Hide in the closet                                                                                                     (B): go to the next door                                                                                                                          (C): look around")
    if answer == "a":
        print("You hide in a closet")
        
    elif answer == "b":
        door += 1
        print("You enter door",door)
    elif answer == "c":
        print("You look around")
    if random.randrange(1,10) == 1:
        print("You found a lighter!")
    #Each item has a number ID
    if invintorySlots == 0:
        invintorySlots = 1
        print("You got a lighter")
    else:
        print("Would you like to replace your current item with this one?")
        answer = input("(A): yes                                                                                                     (B): no
            if answer == "a": 
                print("You swapped your item for a lighter")
            else:
                print("You kept your lighter")
    else:
        answer = input("(A): Go to the next door                                                                                                     (B): say your name                                                                                                                          (C): ...")

    

Solution

  • answer = input("(A): yes
    

    Should be

    answer = input("(A): yes (B): no")
    

    And your if else should not be tabbed over after. You have an extra else statement at the end which will never be reached.

    Your code should look like this:

    import random
    room = random.randrange(100,400)
    door = 0
    closets = 0
    invintorySlots = 0
    # Menu 
    print("                                    --------DOORS--------")
    answer = input("                                   < Press enter to continue >")
    #intro
    print("You enter the hotel, its storming outside")
    print("The receptionist, a girl with black hair and brown eyes says hi to you.")
    print("Her: Your reserved room is number",room) 
    print("  ")
    answer = input("(A): Thanks! (B): Your cute (C): ...")                                                                                                                                                                                                                              
    if answer == "a":
        print("You: Thanks!")
        print("Her: No problem!")
        
    elif answer == "b":
        print("You: Your cute.")
        print("Her: Im not really into dating right now.. Thank you though!")
        
    elif answer == "c":
        print("Her: Not much of a talker eh?")
        print("You walk to your room")
        print("You enter but its not your room. its another room, with another door.")
        #main game
        door += 1
        print("You enter door",door)
        if closets > 0:
            if random.randrange(1,2) == 2:
                print("The lights flicker")
        answer = input("(A): Hide in the closet (B): go to the next door (C): look around")                                                                                                                                                                    
        if answer == "a":
            print("You hide in a closet")
        elif answer == "b":
            door += 1
            print("You enter door",door)
        elif answer == "c":
            print("You look around")
        if random.randrange(1,10) == 1:
            print("You found a lighter!")
        #Each item has a number ID
        if invintorySlots == 0:
            invintorySlots = 1
            print("You got a lighter")
        else:
            print("Would you like to replace your current item with this one?")
            answer = input("(A): yes (B): no")                                                                                          
            if answer == "a": 
                print("You swapped your item for a lighter")
            else:
                print("You kept your lighter")