已解决 | Python:If/elif/else 语句未按预期工作?

Solved | Python: If/elif/else Statement Not Working as Expected?

所以我让用户输入一个表达式:

示例

1 + 1
d * 3
100 / j
s * c

为了允许用户混合使用数字 (int) 和字母 (str),我有以下 if 语句:

user_input = input("Enter a math Equation: ")
user_input = user_input.strip().split(" ")
user_c1 = user_input[0]
user_c2 = user_input[2]
user_c3 = user_input[1]
l1 = user_c1
l2 = user_c2
l3 = user_c2
l6 = user_c1
l4 = int(user_c2)
l5 = int(user_c1)
l7 = int(user_c1)
l8 = int(user_c2)


if l1 and l2 in kb_dxnry:
    print("1st")
elif l3 in kb_dxnry and l4 not in kb_dxnry:
    print("2nd")
elif l5 not in kb_dxnry and l6 in kb_dxnry:
    print("3rd")
else:
    print("4th")

词典代码

user_kb_dxnry = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6,
                 'g': 7, 'h': 8, 'i': 9, 'j': 10, "k": 11, "l": 12,
                 "m": 13, "n": 14, "o": 15, "p": 16, "q": 17, "r": 18,
                 "s": 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24,
                 'y': 25, 'z': 26, "0": 0
                 }
kb_dxnry = user_kb_dxnry

如果我输入(很好地测试它):1 + 1 它将执行 else 语句(因为它应该)。但是,如果您键入任何其他组合:

a + a
1 + a
a + 1

它忽略 if 和 elif 语句并尝试执行 else & failes。

所以我输入

23 * 23

输出:

第四

我输入

a + 1 or a + a or 1 * a

输出为:

ValueError: invalid literal for int() with base 10:

所以我的 if 语句失败了并且是新手(一般编程)我想知道是否有更好的方法来完成我上面想做的事情。也许是我忽略或忽略的内容?

for 会更适合还是会给我同样的错误?

随时 link 告诉我任何您认为可能有助于帮助我完成这项工作的事情。

编辑:

也许这样会更好:

user_input = input("Enter a math Equation: ")
    user_input = user_input.strip().split(" ")
    user_c1 = user_input[0]
    user_c2 = user_input[2]
    user_c3 = user_input[1]
    l1 = user_c1
    l2 = user_c2
    l3 = user_c2
    l6 = user_c1
   user_input = input("Enter a math Equation: ")
user_input = user_input.strip().split(" ")
user_c1 = user_input[0]
user_c2 = user_input[2]
user_c3 = user_input[1]
l1 = user_c1
l2 = user_c2
l3 = user_c2
l6 = user_c1
l4 = user_c2
l5 = user_c1
l7 = user_c1
l8 = user_c2


if l1 in kb_dxnry and l2 in kb_dxnry:
    print("1st")
elif l3 in kb_dxnry and l4 not in kb_dxnry:
    l4 = int(user_c2)
    print("2nd")
elif l5 not in kb_dxnry and l6 in kb_dxnry:
    l5 = int(user_c1)
    print("3rd")
else:
    l7 = int(user_c1)
    l8 = int(user_c2)
    print("4th")

好了,重写上面的代码,ErrorValue 消失了。但是如果你输入 a + 1 或 1 + a

它仍然跳过 elif 1 & 2:

elif l3 in kb_dxnry and l4 not in kb_dxnry:
    print("2nd")
elif l6 in kb_dxnry and l5 not in kb_dxnry:
    print("3rd")

然后跳转到其他地方。

如果您只想处理一个表达式,我不确定为什么要创建这么多变量。

如果您查看错误语句中的行号,我敢打赌您会看到它指的是您定义 l4, l5, l7, l7 的行。您正在那里进行 int() 转换,如果您将这些转换传递给一个字符串,您将得到您发布的错误,所以我认为您甚至没有得到您的 if-else 语句。

好吧,我在@AirSquid 的帮助下解决了这个问题。我删除了 str 到 int 并在字典中搜索可能是数字的字符串。比起 if 语句,我会将它们更改为 int。

user_input = input("Enter a math Equation: ")
    user_input = user_input.strip().split(" ")
    user_c1 = user_input[0]
    user_c2 = user_input[2]
    user_c3 = user_input[1]
    l1 = user_c1
    l2 = user_c2
    l3 = user_c2
    l6 = user_c1
   user_input = input("Enter a math Equation: ")
user_input = user_input.strip().split(" ")
user_c1 = user_input[0]
user_c2 = user_input[2]
user_c3 = user_input[1]
l1 = user_c1
l2 = user_c2
l3 = user_c2
l6 = user_c1
l4 = user_c2
l5 = user_c1
l7 = user_c1
l8 = user_c2


if l1 in kb_dxnry and l2 in kb_dxnry:
    print("1st")
elif l3 in kb_dxnry:
    l4 = int(user_c2)
    print("2nd")
elif l5 not in kb_dxnry:
    l5 = int(user_c1)
    print("3rd")
else:
    l7 = int(user_c1)
    l8 = int(user_c2)
    print("4th")

上面的代码现在完成了我想要它做的事情。