Python仅读取if语句,而不读取任何elif语句

时间:2019-02-18 14:47:41

标签: python python-3.x

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

我在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语句。

1 个答案:

答案 0 :(得分:0)

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

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

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