簡體   English   中英

如何解決代碼中的 KeyError,我是初學者

[英]How can I solve a KeyError in my code, I'm a begginer

我正在解決一個基本問題,包括制作產品清單、用戶選擇產品和產品數量以及打印總價。 我在第 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”。

當您使用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)

python中的input以字符串形式接收數據,您需要對其進行類型轉換

它是一些東西:

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

我認為在請求用戶輸入時,您還應該遵循 Python 習語EAFP(請求寬恕比許可更容易) ,因為他可以寫除您期望的整數之外的所有內容:

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
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)

您需要將輸入作為 integer 因為 input() 將默認值作為字符串,因此您可以像 quantify quantify = int(input("Enter amount: "))或另一種方法是在該地方使用 int()其中計算類似於totalPrice = float(relation[int(code)] * int(quantify))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM