繁体   English   中英

Python 只读取 if 语句而不读取任何 elif 语句

[英]Python only reading the if statement not any of the elif statements

我正在用 python 开发一个老式的冒险游戏,当我运行将 +1 或 -1 添加到 X 或 Y 的移动命令时,它不会检查 elif 语句。

我在 while True 之后添加了一个打印,发现代码只检查第一个语句是否为真,而不检查它下面的 elif 语句。 我知道这些值会发生变化,但它不使用任何 elif 语句检查它们,它只检查 if 语句。

while True:
    if X == "0" and Y == "0":
        inp = input('You are in a dark room and cannot see much. describe items, collect, move')
        if inp == "describe items":
            print('The room has a key, and two doors.')
        if inp == "collect":
            Icollect = input('Which item do you want to collect')
            if Icollect == "key":
                Slot = input('You have 5 inventory slots, which one do you want to put the key into?')
                Inventory[1] = 'key'
        if inp == "move":
            Move = input('Which door do you want to go through the door in front or to the right of you?')
            if Move == "front":
                Y = int(Y) + 1
            if Move == "right":
                X = int(X) + 1
    elif X == "0" and Y == "1":
        inp = input('You have climbed a set of stairs and entered what looks like a study. describe items, inspect, move')
        if inp == "describe items":
            print('There is a bookcase and a door to the left and behind you.')
        if inp == "inspect":
            inspect = input('You can only inspect the bookcase')
            if inspect == "bookcase":
                print('The bookcase has fallen and revealed a third door to your right')
        if inp == "move":
            Move = input('Which door do you want to go through the door to the left or behind you?')
            if Move == "behind":
                Y = int(Y) - 1
            if Move == "left":
                X = int(X) - 1
            if Move == "right":
                X = int(X) + 1
    elif X == "1" and Y == "0":
        inp = input('You went down a spiral staircase and entered a dungeon. describe items, collect, move')
        if inp == "describe items":
            print('The room has a key, and two doors.')
        if inp == "collect":
            Icollect = input('Which item do you want to collect')
            if Icollect == "key":
                Slot = input('You have 5 inventory slots, which one do you want to put the key into?')
                Inventory[1] = 'key'
        if inp == "move":
            Move = input('Which door do you want to go through the door in front or to the right of you?')
            if Move == "front":
                Y = int(Y) + 1
            if Move == "right":
                X = int(X) + 1

我预计代码会更改 X 和 Y,并且会继续使用其他 elif 语句,因为那个语句不再正确。 代码更改了值,但它从不检查其他语句,它只检查一个 if 语句。

如果我理解正确,您正在更改变量的类型

X = "0"  
X = int(X) + 1
if X=="1":
   print("equals 1")

这是错误的,因为 type(X) 现在是 int

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM