python 3 中的 try 和 except 函数使我的代码无法按预期运行
My code is not functioning as expected with the try and except function within python 3
所以我遇到了一个关于 Python 的奇怪问题,它与 try 和 except 函数有关。它应该允许我重新输入客户,在这种情况下是 c2,因为 c1 正在将资金转移到 c2。但是它给了我一个键盘错误,而不是说用户不在数据库中并要求我重新输入客户名称。尽管存在视觉错误而不是功能错误,但在下面的代码中第二次使用 try 和 except 仍然有效。
我试过更改使用 try 和 except 的位置,并尝试在线搜索但没有找到解决方案。我只做了 Python 几个月,而且一直断断续续。
elif option == 3:
print("Option", 3)
try:
c2 = input("Customer 2")
customer2 = BankSystem.c[c2]
except ValueError:
print("\n>>>An exception occured ~ invalid i/p")
print("\t~Customer not in database")
print()
if "c2" in BankSystem.c: ## check for valid account id
print(customer2)
try:
amount = float(input("Amount to transfer "))
self.tranfer(customer2, amount)
except ValueError:
print("\n>>>An exception occured ~ invalid i/p")
print("\t~Non-numeric data entered")
else:
print("\n>>>>>>>account:{} does not
exist".format("c2"))
print(self)
print(customer2)
根据文档,"Python raises a KeyError whenever a dict() object is requested (using the format a = adict[key]) and the key is not in the dictionary."因此,您应该检查 except 块中的键错误:
try:
c2 = ....
except KeyError:
print("\n>>>An exception occured ~ invalid i/p")
print("\t~Customer not in database")
编辑:
您可以将 try/except 重组为:
try:
c2 = input("Customer 2")
customer2 = BankSystem.c[c2]
if "c2" in BankSystem.c: ## check for valid account id
print(customer2)
amount = float(input("Amount to transfer "))
self.tranfer(customer2, amount)
else:
print("\n>>>>>>>account:{} does not exist".format("c2"))
print(self)
print(customer2)
except KeyError:
print("\n>>>An exception occured ~ invalid i/p")
print("\t~Customer not in database")
except ValueError:
print("\n>>>An exception occured ~ invalid i/p")
print("\t~Non-numeric data entered")
it gives me a keyerror
这是因为在这一行
customer2 = BankSystem.c[c2]
您正在尝试使用密钥 c2
(由用户提供)访问 dict
。如果没有这样的键 KeyError
就会发生。请注意,您可以检查 dict
是否具有以下方式的密钥,而不是使用 try-except
:
d = {'a':1}
k = input('key:')
if k in d.keys():
print('key present')
else:
print('key not present')
如果你给它 a
和 key not present
在所有其他情况下,将打印 key present
。
所以我遇到了一个关于 Python 的奇怪问题,它与 try 和 except 函数有关。它应该允许我重新输入客户,在这种情况下是 c2,因为 c1 正在将资金转移到 c2。但是它给了我一个键盘错误,而不是说用户不在数据库中并要求我重新输入客户名称。尽管存在视觉错误而不是功能错误,但在下面的代码中第二次使用 try 和 except 仍然有效。
我试过更改使用 try 和 except 的位置,并尝试在线搜索但没有找到解决方案。我只做了 Python 几个月,而且一直断断续续。
elif option == 3:
print("Option", 3)
try:
c2 = input("Customer 2")
customer2 = BankSystem.c[c2]
except ValueError:
print("\n>>>An exception occured ~ invalid i/p")
print("\t~Customer not in database")
print()
if "c2" in BankSystem.c: ## check for valid account id
print(customer2)
try:
amount = float(input("Amount to transfer "))
self.tranfer(customer2, amount)
except ValueError:
print("\n>>>An exception occured ~ invalid i/p")
print("\t~Non-numeric data entered")
else:
print("\n>>>>>>>account:{} does not
exist".format("c2"))
print(self)
print(customer2)
根据文档,"Python raises a KeyError whenever a dict() object is requested (using the format a = adict[key]) and the key is not in the dictionary."因此,您应该检查 except 块中的键错误:
try:
c2 = ....
except KeyError:
print("\n>>>An exception occured ~ invalid i/p")
print("\t~Customer not in database")
编辑: 您可以将 try/except 重组为:
try:
c2 = input("Customer 2")
customer2 = BankSystem.c[c2]
if "c2" in BankSystem.c: ## check for valid account id
print(customer2)
amount = float(input("Amount to transfer "))
self.tranfer(customer2, amount)
else:
print("\n>>>>>>>account:{} does not exist".format("c2"))
print(self)
print(customer2)
except KeyError:
print("\n>>>An exception occured ~ invalid i/p")
print("\t~Customer not in database")
except ValueError:
print("\n>>>An exception occured ~ invalid i/p")
print("\t~Non-numeric data entered")
it gives me a keyerror
这是因为在这一行
customer2 = BankSystem.c[c2]
您正在尝试使用密钥 c2
(由用户提供)访问 dict
。如果没有这样的键 KeyError
就会发生。请注意,您可以检查 dict
是否具有以下方式的密钥,而不是使用 try-except
:
d = {'a':1}
k = input('key:')
if k in d.keys():
print('key present')
else:
print('key not present')
如果你给它 a
和 key not present
在所有其他情况下,将打印 key present
。