为什么我的代码中与另一个工作部分相同的部分不起作用

时间:2018-10-18 13:00:57

标签: python python-2.7 if-statement raw-input

im使用python脚本2.7.15并显示“您的程序中存在错误:语法无效”,然后在第21行中用红色突出显示了一个:,我不明白为什么:这将是错误的,因为它没有在第21行之前的任何代码都存在问题,尽管它们相同。 我的代码如下:

def rooms(): 
print "You're in a room and you see 2 doors in front of you."
print "Do you take the door on the left or the right?"
answer = raw_input("Type left or right and hit 'Enter'.")
if answer == "left":
    print "You go through the door on the left and find youreself in a room with two buttons"
    print "Do you press the green button or the yellow button"
    answer = raw_input("Type green or yellow and hit 'Enter'.")
    if answer == "green":
        print "You press the green button and a trapdoor opens from beneath you and you fall into a small room."
        print "In that room you see a switch and a door, do you pull the switch or go trough the door?"
        answer = raw_input("Type switch or door and hit 'Enter'.")
        if answer == "switch":
            print "You pull the switch, you hear the door lock and the room starts filling with water and you drown."
            print "The end.    Congrats you got the 'drowned endng' ending 1 out of 8" # drowned ending
        elif answer == "door":
            print "You go through the door and you're in a hallway"
            print "You start walking through the hallway and you come to a halt when the hallway splits in two"
            print "Do you take the hallway on the left or the hallway on the right?"
            answer = raw_input("Type left or right and hit 'Enter'."
            if answer == "left": #why does it get a problem here
                print "You start walking through the left hallway, the hallway ends with a door and you go through the door."
                print "The room you step into is filled with cake and you choose to stay here for ever."
                print "The end    Congrats you got the 'cake ending' ending 2 out of 7" #cake ending
            elif anwser == "right":
                print "You start walking in the right hallway, the hallway end and you see the outsideworld."
                print "You're finally free"
                print "The end    Congrats you got the 'freedom ending' ending 3 out of 8" #freedom ending
            else:
                print "You didn't choose Game over"
        else:
            print "You didn't choose Game over"
    elif answer == "yellow":
        print "You press the yellow button and a previously hidden door opens."
        print "You go through the door and it locks behind you, you see 2 keys laying on a desk and a locked door next to it."
        print "Do you pick up the big key or the small key?"
        answer = raw_input("Type big or small and press 'Enter'."
        if answer == "big":
            print "You pick up the big key and the small key disappears, you try to unlock the door using the big key."
            print "It doesnt work and you're stuck there for ever."
            print "The end    Congrats you got the 'stuck for ever ending' ending 4 out of 8" #stuck forever ending
        if answer == "small":
            print "You pick up the small key and the big key disappears, you unlock the door using the small key.
            print "You step through the door and you see a big castle with townspeople that bow for you"
            print "The end    Congrats you got the 'king ending' ending 5 out of 8" #king ending
        else:
            print "You didn't choose Game over"
    else:
        print "You didn't choose Game over"
elif answer == "right":
    print "You go through the door on the left and see 2 vegetables laying on a late with a note."
    print "You read the note and it says 'one will kill and one will save, wich will you choose"
    print "Do you choose the carrot or the broccoli?"
    answer = raw_input("type carrot or broccoli and hit 'Enter'."
    if answer == "carrot":
        print "You eat the carrot and you suddenly get teleported to another room."
        print "In that room you see a computer keyboard that's missing all keys except the delete and enter key."
        print "Will you press the delete button or the enter button?"
        answer = raw_input("type delete or enter and press 'Enter'.")
        if answer == "delete":
            print "You press the delete button and you fade out of existence"
            print "The end    Congrats you got the 'deleted ending' ending 6 out of 8" #deleted ending
        elif answer == "enter":
            print "You press the enter button and suddenly a computer screen appears with a person in a room with two doors."
            print "The end    Congrats you got the 'fourth wall ending' ending 7 out of 8" #fourth wall ending
        else:
            print "You didn't choose Game over."
    if answer == "broccoli":
        print "You eat the broccoli and die"
        print "The end    Congrats you got the 'poisonded ending' ending 8 out of 8" #poisoned ending
else:
    print "You didn't choose Game over"
    rooms()

rooms()

1 个答案:

答案 0 :(得分:0)

您忘记在程序中关闭raw_input几次。函数raw_input应该以{{1​​}}关闭。