[英]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 & 失敗。
所以我輸入
23 * 23
Output :
第 4 名
我打字
a + 1 or a + a or 1 * a
Output 是:
ValueError: invalid literal for int() with base 10:
所以我的 if 語句失敗並且是新手(一般編程)我想知道是否有更好的方法來做我上面想做的事情。 也許我看過或看不到的東西?
a for 會更適合還是會給我同樣的錯誤?
隨時將我鏈接到您認為可能有助於幫助我完成這項工作的任何事情。
編輯:
也許這樣會更好:
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")
上面的代碼現在完成了我想要它做的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.