【问题标题】:Try / Except Statements in python [closed]python中的try / except语句[关闭]
【发布时间】:2018-08-18 15:17:07
【问题描述】:

我正在尝试创建一个使用 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: ['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'

【问题讨论】:

  • 哪一行出错?
  • 怀疑拼写错误 → 题外话。 Exception 不是 Excetption
  • 我怀疑您没有将try: ... except: ... 放在正确的代码周围,并且可能会从您那里得到ValueErrorint()float() 电话,因为我看不到您的代码如何包裹在try, except 中会导致raise 异常。
  • 只有当它到达那行代码时,拼写错误才会引发NameError
  • 您的编辑没有修正Excetption的两个拼写错误

标签: python try-except


【解决方案1】:
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)

这里!我检查了您的代码并发现以下内容,a)您拼错了“异常”,b)存在逻辑错误。您首先询问号码,然后使用语句 try。您必须在 try 语句中输入内容,否则您的程序什么也不会尝试

检查我的代码,我在 try: 中有输入语句,如果一切正常,那么它将继续询问其他输入并根据参考编号是否在列表中进行设置

【讨论】:

  • 谢谢它很好用。
猜你喜欢
  • 1970-01-01
  • 2013-04-10
  • 2016-09-23
  • 2020-11-20
  • 2011-09-29
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多