所以我们这门课的最终项目是制作一个基于控制台文本的益智游戏,并且出于某种原因,在尝试将 ifs 和 elifs 放入 while 循环时,它会自动缩进 if 下的 elif。我尝试删除缩进,当我去测试它时,出现缩进错误:需要一个缩进块。所以我尝试添加缩进,这看起来很奇怪,因为这是第一次发生这种情况。然后我得到了一个简单的语法错误。
尝试删除缩进:收到缩进错误 尝试包括缩进:收到语法错误 在这一点上我真的迷路了。如果它有帮助,我们被告知使用 replit 来提交我们的代码。我也在使用 time、sys、os 和 termcolor 模块。粗体是有错误的行。
`def airlock(inventory):
clear()
userin = None
inventory["note"] = "your astronaut ID is: 2579"
while userin != "quit":
time.sleep(1.5)
print("description of airlock, keypad on wall next to door")
print("what u wanna do? 1 look around 2 check pockets 3 go to keypad")
userin = input()
if userin == "1":
#describe the keypad. there is nothing else in the room
**elif userin == "2":
invinspect(inventory)**
def invinspect(inventory):
userin = None
print(inventory.keys())
print("what would you like to do? 1. look closer 2 go back")
userin = input()
if userin == 1:
print(str(inventory))
else:
def main():
inventory = {}
airlock(inventory)
main()`
如评论中所述,如果在python中引入条件语句,那么它后面必须有某种语句。这可以是像
pass
这样的“虚拟”语句,但它不能是评论(它们不算数)。
例子
x = 5
if x > 4: # GOOD
print('bigger than 4')
if x > 4: # GOOD
pass
if x > 4: # ERROR
# a comment
试试这个。您有 2 个位置有条件但没有下一行。当您遇到错误时,请务必对行号进行质量检查。请注意,我注释掉了
clear()
,因为它未定义
import time
def airlock(inventory):
#clear()
userin = None
inventory["note"] = "your astronaut ID is: 2579"
while userin != "quit":
time.sleep(1.5)
print("description of airlock, keypad on wall next to door")
print("what u wanna do? 1 look around 2 check pockets 3 go to keypad")
userin = input()
if userin == "1":
pass
#describe the keypad. there is nothing else in the room
elif userin == "2":
invinspect(inventory)
def invinspect(inventory):
userin = None
print(inventory.keys())
print("what would you like to do? 1. look closer 2 go back")
userin = input()
if userin == 1:
print(str(inventory))
else:
pass
def main():
inventory = {}
airlock(inventory)
main()
if userin == "1":
#describe the keypad. there is nothing else in the room
elif userin == "2":
invinspect(inventory)