我是一个初学者,如何解决代码中的KeyError

时间:2019-07-19 00:56:45

标签: python python-3.x keyerror

我正在解决一个基本问题,包括列出产品清单,用户选择产品和产品数量以及打印总价。我在第22行遇到键盘错误。

def main():
   print("Choose a product: ")
    print("")
    print("Products: ")
    print("")
    print("Samsung Galaxy S10+.............1")
    print("Samsung Galaxy S10..............2")
    print("OnePlus 7 Pro...................3")
    print("OnePlus 7.......................4")
    print("OnePlus 6t......................5")
    print("Huawei P30 Pro..................6")
    print("Huawei Mate 20 Pro..............7")
    print("Google Pixel 3XL................8")
    print("Gooogle Pixel 3A XL.............9")
    print("Oppo Reno 10x Zooom............10")
    print("")

    relation = {1:1000, 2:900, 3:700, 4:600, 5:470, 6:850, 7:970, 8:950, 9:300, 10:550}

    code = input("Enter the product code: ")
    print("")
    print("The price is $", relation[code])
    quantify = input("Enter amount: ")
    print("")

    totalPrice = float(relation[code] * quantify)

    print("The total price is: $", totalPrice)

显示的错误是

Traceback (most recent call last):
  File "main.py", line 30, in <module>
    main()
  File "main.py", line 22, in main
    print("The price is $", relation[code])
KeyError: '5'

在这种情况下,我选择产品代码“ 5”。

4 个答案:

答案 0 :(得分:1)

使用input时,它将返回一个字符串,而不是整数。您会看到此消息,因为错误消息显示的是'5',而不是5。但是,字典的键是整数,因此找不到在语句(code)中提供的键。 您可以改用

print("The price is $", relation[int(code)])

至少在Python 3.6和更高版本中,更好的格式应该是

print(f"The price is ${relation[int(code)]}")

对于第26行,问题类似。只需转换为整数(或浮点数,如果有小数点的话)

totalPrice = float(relation[int(code)] * int(quantify))

totalPrice = relation[int(code)] * float(quantify)

答案 1 :(得分:0)

python中的

input以字符串形式接收数据,您需要对其进行类型转换

这是沿着:

print("The price is $", relation[int(code)])

答案 2 :(得分:0)

我认为您在请求用户输入时也应该遵循Python习惯用法EAFP (Easier to ask for forgiveness than permission),因为他可以写出除期望的整数以外的所有内容:

while True:
    code = input("Enter the product code: ")

    try:
        price = relation[int(code)]
    except (ValueError, KeyError):
        print("Error: Incorrect code value, try again!")
    else:
        break

答案 3 :(得分:0)

def main():
    print("Choose a product: ")
    print("")
    print("Products: ")
    print("")
    print("Samsung Galaxy S10+.............1")
    print("Samsung Galaxy S10..............2")
    print("OnePlus 7 Pro...................3")
    print("OnePlus 7.......................4")
    print("OnePlus 6t......................5")
    print("Huawei P30 Pro..................6")
    print("Huawei Mate 20 Pro..............7")
    print("Google Pixel 3XL................8")
    print("Gooogle Pixel 3A XL.............9")
    print("Oppo Reno 10x Zooom............10")
    print("")

    relation = {1:1000, 2:900, 3:700, 4:600, 5:470, 6:850, 7:970, 8:950, 9:300, 10:550}

   code = input("Enter the product code: ")
   print("")
   print("The price is $", relation[code])
   quantify = input("Enter amount: ")
   print("")

   totalPrice = float(relation[int(code)] * quantify)

   print("The total price is: $", totalPrice)