If语句帮助Python
为什么这总是导致“Vault错误”?我环顾四周,感觉我的if条件是错误的,但我不确定。输入方法以字符串类型返回用户输入,您尝试比较两种不同的类型(int和str) 将比较更改为str,例如:If语句帮助Python,python,python-3.x,Python,Python 3.x,为什么这总是导致“Vault错误”?我环顾四周,感觉我的if条件是错误的,但我不确定。输入方法以字符串类型返回用户输入,您尝试比较两种不同的类型(int和str) 将比较更改为str,例如: keyCounter = 0 key1Value = 0 key2Value = 0 key3Value = 0 print(key1Value) key1Value = input("Press the first key.") key2Value = input("Press the second
keyCounter = 0
key1Value = 0
key2Value = 0
key3Value = 0
print(key1Value)
key1Value = input("Press the first key.")
key2Value = input("Press the second key.")
key3Value = input("Press the third key.")
# password = 123
if key1Value == 1 and key2Value == 2 and key3Value == 3:
print("Access Granted")
print(key1Value)
print(key2Value)
print(key3Value)
elif key1Value != 1 and \
key2Value != 2 and \
key3Value != 3:
print("Access Denied")
print(key1Value)
print(key2Value)
print(key3Value)
else:
print("Vault error")
print(key1Value)
print(key2Value)
print(key3Value)
input("Press Enter to continue...")
您还可以将输入转换为int:
if key1Value == "1" and key2Value == "2" and key3Value == "3":
请注意,如果不插入实际数字,则会出现错误
总之,我将做以下几点:
key1Value = int(input("Press the first key."))
通过这种方式,您可以检查用户输入是否为int类型,如果不是int类型,您可以以适当的方式处理它。因为
input
总是返回字符串。换句话说,您试图将1
与'1'
进行比较。使用int(输入(“按第一个键”)
。如果输入类似126
(其中两个值匹配,但一个不匹配)的代码,则会出现“Vault错误”,而不是“拒绝访问”。只有当所有数字都不匹配时,您才会看到“拒绝访问”。我不确定这是否是你想要的结果。这在这里可能不是问题,但我只想指出:key1Value==1和key2Value==2和key3Value==3的反比实际上是key1Value!=1或key2Value!=2或key3Value!=3
(这是由于)。这很有道理。谢谢
try:
value=int(input("Press the first key."))
except ValueError:
print("This is not a whole number.")