我有一个语法错误,我对该怎么做不知道



我在为初学者的编程书中编写代码。这就是它的样子。(我必须扩展此帖子,因为此网站对问题帖子非常挑剔。)

import random
print("You are in a dark room in a mysterious castle.")
print("In front of you are four doors. You must choose one.")
playerChoice = input("Choose 1, 2, 3, or 4...")
if playerChoice == "1":
        print("You found a room full of tresure. YOU'RE RICH!!!")
        print("GAME OVER, YOU WIN!")
elif playerChoice == "2":
        print("The door opens and an angry ogre hits you with his club.")
        print("GAME OVER, YOU DIED!")
elif playerChoice == "3":
        print("You encounter a sleeping dragon.")
        print("You can do either:")
        print("1) Try to steal some of the dragon's gold")
        print("2) Sneak around the dragon to the exit")
        dragonChoice = input("type 1 or 2...")
        if dragonChoice == "1":
            print("The dragon wakes up and eats you. You are delicious.")
            print("GAME OVER, YOU WERE EATEN ALIVE.")
        elif dragonChoice == "2":
            print("You sneak around the dragon and escape the castle, blinking in the sunshine.")
            print("GAME OVER, YOU ESCAPED THE CASTLE.")
        else:
                print("Sorry, you didn't enter 1 or 2.")
elif    playerChoice == "4":
            print("You enter a room with a sphinx.")
            print("It asks you to guess what number it is thinking of, betwwen 1 to 10.")
            number = int(input("What number do you choose?")
        if number == random.randint (1, 10)
                print("The sphinx hisses in dissapointment. You guessed correctly.")
                print("It must let you go free.")
                print("GAME OVER, YOU WIN.")             
        else:
             print("The sphinx tells you that your guess is incorrect.")
             print("You are now it's prisoner forever.")
             print("GAME OVER, YOU LOSE.")        
        else:
         print("sorry, you didn't enter 1, 2, 3, or 4...")
         print("YOU TURN BACK AND LEAVE (YOU COWARD)")

这是您的两个问题:

number = int(input("What number do you choose?")
if number == random.randint (1, 10)

int的演员缺失了闭合括号,而 if语句缺失了一个结肠。

最后一个问题与最后的else语句有关。假设这是您想要的,那么最后一个。

修复:

import random
print("You are in a dark room in a mysterious castle.")
print("In front of you are four doors. You must choose one.")
playerChoice = input("Choose 1, 2, 3, or 4...")
if playerChoice == "1":
    print("You found a room full of tresure. YOU'RE RICH!!!")
    print("GAME OVER, YOU WIN!")
elif playerChoice == "2":
    print("The door opens and an angry ogre hits you with his club.")
    print("GAME OVER, YOU DIED!")
elif playerChoice == "3":
    print("You encounter a sleeping dragon.")
    print("You can do either:")
    print("1) Try to steal some of the dragon's gold")
    print("2) Sneak around the dragon to the exit")
    dragonChoice = input("type 1 or 2...")
    if dragonChoice == "1":
        print("The dragon wakes up and eats you. You are delicious.")
        print("GAME OVER, YOU WERE EATEN ALIVE.")
    elif dragonChoice == "2":
        print(
            "You sneak around the dragon and escape the castle, blinking in the sunshine.")
        print("GAME OVER, YOU ESCAPED THE CASTLE.")
    else:
        print("Sorry, you didn't enter 1 or 2.")
elif playerChoice == "4":
    print("You enter a room with a sphinx.")
    print("It asks you to guess what number it is thinking of, betwwen 1 to 10.")
    number = int(input("What number do you choose?"))
    if number == random.randint(1, 10):
        print("The sphinx hisses in dissapointment. You guessed correctly.")
        print("It must let you go free.")
        print("GAME OVER, YOU WIN.")
    else:
        print("The sphinx tells you that your guess is incorrect.")
        print("You are now it's prisoner forever.")
        print("GAME OVER, YOU LOSE.")
else:
    print("sorry, you didn't enter 1, 2, 3, or 4...")
    print("YOU TURN BACK AND LEAVE (YOU COWARD)")

python是一种划界语言,这意味着您需要特别注意凹痕。您的缩进是不一致的,这将不可避免地导致问题。

除此之外,您没有指定您的错误实际是什么。将语法错误信息添加到您的帖子中。

您只是在间距和忘记时遇到了一些问题:"在第30行

import random
print("You are in a dark room in a mysterious castle.")
print("In front of you are four doors. You must choose one.")
playerChoice = input("Choose 1, 2, 3, or 4...")
if playerChoice == "1":
        print("You found a room full of tresure. YOU'RE RICH!!!")
        print("GAME OVER, YOU WIN!")
elif playerChoice == "2":
        print("The door opens and an angry ogre hits you with his club.")
        print("GAME OVER, YOU DIED!")
elif playerChoice == "3":
        print("You encounter a sleeping dragon.")
        print("You can do either:")
        print("1) Try to steal some of the dragon's gold")
        print("2) Sneak around the dragon to the exit")
        dragonChoice = input("type 1 or 2...")
        if dragonChoice == "1":
            print("The dragon wakes up and eats you. You are delicious.")
            print("GAME OVER, YOU WERE EATEN ALIVE.")
        elif dragonChoice == "2":
            print("You sneak around the dragon and escape the castle, blinking in the sunshine.")
            print("GAME OVER, YOU ESCAPED THE CASTLE.")
        else:
            print("Sorry, you didn't enter 1 or 2.")
elif    playerChoice == "4":
            print("You enter a room with a sphinx.")
            print("It asks you to guess what number it is thinking of, betwwen 1 to 10.")
            number = int(input("What number do you choose?"))    
            if number == random.randint (1, 10):
                print("The sphinx hisses in dissapointment. You guessed correctly.")
                print("It must let you go free.")
                print("GAME OVER, YOU WIN.")             
            else:
                 print("The sphinx tells you that your guess is incorrect.")
                 print("You are now it's prisoner forever.")
                 print("GAME OVER, YOU LOSE.")        
else:
     print("sorry, you didn't enter 1, 2, 3, or 4...")
     print("YOU TURN BACK AND LEAVE (YOU COWARD)")

相关内容