python中的Try/Except语句
我正在尝试创建一个使用try/except语句的python程序。一切正常,除了,我仍然得到一个值错误,即使在定义了except语句之后 我的代码:python中的Try/Except语句,python,try-except,Python,Try Except,我正在尝试创建一个使用try/except语句的python程序。一切正常,除了,我仍然得到一个值错误,即使在定义了except语句之后 我的代码: inventory = {847502: ['APPLES 1LB', 1.99, 50], 847283: ['OLIVE OIL', 10.99, 100], 839529: ['TOMATOS 1LB', 1.29, 25], 483946: ['MILK 1/2G', 3.45, 35], 493402:
inventory = {847502: ['APPLES 1LB', 1.99, 50], 847283: ['OLIVE OIL', 10.99, 100], 839529: ['TOMATOS 1LB', 1.29, 25],
483946: ['MILK 1/2G', 3.45, 35], 493402: ['FLOUR 5LB', 2.99, 40], 485034: ['BELL PEPPERS 1LB', 1.35, 28],
828391: ['WHITE TUNA', 1.69, 100], 449023: ['CHEESE 1/2LB', 4.99, 15]}
upc_input = int(input('Enter a UPC number: '))
description = input('Enter a, item description: ')
unit_price = float(input('Enter the unit price: '))
quantity = int(input('Enter th quantity: '))
try:
if upc_input in inventory.keys():
print('Inventory Updating')
inventory[upc_input] = [description,unit_price,quantity]
except Excetption as exp:
print('Error occurred!',exp)
try:
if upc_input not in inventory.keys():
print('Adding new inventory')
inventory[upc_input] = [description,unit_price,quantity]
except Excetption as exp:
print('Error occurred!',exp)
print()
print(inventory)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-17-1313cc5c500f> in <module>()
----> 1 upc_input = int(input('Enter a UPC number: '))
2 description = input('Enter a, item description: ')
3 unit_price = float(input('Enter the unit price: '))
4 quantity = int(input('Enter th quantity: '))
5
ValueError: invalid literal for int() with base 10: 'eeeee'
这里!我检查了你的代码,发现如下,a你错发了异常,b有一个逻辑错误。你先要电话号码,然后用try语句。您必须在try语句中输入,否则您的程序将什么也不尝试
签入我的代码我在try:中有输入语句,如果一切正常,那么它将继续请求其他输入,并根据参考号是否在列表中设置它们哪一行出错?怀疑输入错误→ 离题。异常不例外。我怀疑您没有尝试:。。。除了:。。。大概是正确的代码,可能会从int或float调用中得到一个ValueError,因为我看不出您包装在try中的代码是如何产生异常的。只有当输入到该行代码时,输入错误才会产生NameError。您的编辑没有用excetpionThank修复这两个输入错误中的任何一个。谢谢,它工作得很好。
inventory = {847502: ['APPLES 1LB', 1.99, 50], 847283: ['OLIVE OIL', 10.99, 100], 839529: ['TOMATOS 1LB', 1.29, 25],
483946: ['MILK 1/2G', 3.45, 35], 493402: ['FLOUR 5LB', 2.99, 40], 485034: ['BELL PEPPERS 1LB', 1.35, 28],
828391: ['WHITE TUNA', 1.69, 100], 449023: ['CHEESE 1/2LB', 4.99, 15]}
while True:
try:
upc_input = int(input('Enter a UPC number: '))
break
except Exception:
print("Oops! That was no valid number. Try again...")
if upc_input in inventory.keys():
description = input('Enter a, item description: ')
unit_price = float(input('Enter the unit price: '))
quantity = int(input('Enter th quantity: '))
print('Inventory Updating')
inventory[upc_input] = [description,unit_price,quantity]
if upc_input not in inventory.keys():
description = input('Enter a, item description: ')
unit_price = float(input('Enter the unit price: '))
quantity = int(input('Enter th quantity: '))
print('Adding new inventory')
inventory[upc_input] = [description,unit_price,quantity]
print()
print(inventory)