在使用Python的IDLE制作文本故事时出现“预期缩进错误”

2024-10-01 11:33:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用python编程时收到idle发出的“预期缩进错误”。我在试着编一个文字故事。你知道吗

这是我的密码:

import time
choice1 = input("you walk into a haunted house, do you take door #1 or door #2?(1/2)")
if choice1 == "1":
        door1 = input("You see a staircase... go up or down? (up/down)")
        if door1 == ("up"):

        if door1 == ("down"):
            print ("AHHHHHHHH.....")
            time.sleep(1)
            print ("AHHHHHHHH.....")
            time.sleep(1)print (".........")
            time.sleep(1)
            print("THUNK. you slipped off the ladder and died because you fell for so long")
if choice1 == "2":
        print ("ahhhhhhhhhhhhhh")
        time.sleep(1)
        print("oooff, you fell into a dark room, try to find your flashlight or wander around? (flashlight/wander)")

Tags: oryouinputiftimesleepdownprint
3条回答

每个块下面都应该有代码

if door1 == ("up"): 

可以更改为

if door1 == ("up"): 
     pass

您必须小心缩进,python在缩进中很严重,您必须在if语句之后添加一些语句,尝试使用pass。此外,它将每个句子分隔成一行。你知道吗

import time

choice1 = input("you walk into a haunted house, do you take door #1 or door #2?(1/2)")

if choice1 == "1":
        door1 = input("You see a staircase... go up or down? (up/down)")
        if door1 == ("up"):
            pass
        if door1 == ("down"):
            print ("AHHHHHHHH.....")
            time.sleep(1)
            print ("AHHHHHHHH.....")
            time.sleep(1)
            print (".........")
            time.sleep(1)
            print("THUNK. you slipped off the ladder and died because you fell for so long")
if choice1 == "2":
        print ("ahhhhhhhhhhhhhh")
        time.sleep(1)
        print("oooff, you fell into a dark room, try to find your flashlight or wander around? (flashlight/wander)")

发生错误的原因是您的块中没有语句。你知道吗

Python语法需要在if、except、def、class等后面加上代码块

这里,if door1 == ("up"):是空的。你知道吗

如果不需要在代码块中执行任何操作,则在这样的块中包含pass以不生成IndentationError。你知道吗

pass语句不执行任何操作。当在语法上需要一个语句,但程序不需要任何操作时,可以使用它。你知道吗

在代码中,使用

if door1 == ("up"): 
     pass

相关问题 更多 >