簡體   English   中英

為什么我不斷收到關於此字符串語句的錯誤輸入錯誤?

[英]Why do i keep getting a bad input error regarding this string statement?

我有一個語法錯誤,我在python代碼中感到困惑

deliveryValue= input("Is this order for delivery? Y/N")
float (DeliveryTime = 0)
str (zipcode=none)
float (DeliveryFee =0)
if deliveryValue == 'y' or 'Y':
    address = input ("what is the delivery address?")
    zipcode = input ("What zipcode is the address in?")
        if zipcode == 84041:
            DeliveryTime = 30
            DeliveryFee = 6.50
        else:
            DeliveryTime = 45
            DeliveryFee = 7.50
else:
    DeliveryFee = 0

if郵政編碼== 84041:該行出現錯誤,但是我嘗試在其周圍放置''和'“,但仍然存在相同的錯誤。 我寫的正確嗎?

提前致謝。

嘗試將if語句更改為

if deliveryValue in 'yY':

你也可以使用

if deliveryValue == 'y' or deliveryValue  == 'Y':

一些問題:

  • 您的分配不應包含在類型的構造函數中。
  • 條件條件中if語句的表達式始終為True因為'Y'始終為True 如果要不區分大小寫地比較字符串,請在字符串變量上使用lower()方法。
  • 郵政編碼比較的if語句比應縮進的深度高一級。
  • zipcode的值是一個字符串,因為它是input()函數的返回值。 因此,您應該將其與字符串而不是整數進行比較。

修復了上述問題后,您的代碼應如下所示:

deliveryValue= input("Is this order for delivery? Y/N")
DeliveryTime = 0
zipcode = None
DeliveryFee = 0
if deliveryValue.lower() == 'y':
    address = input ("what is the delivery address?")
    zipcode = input ("What zipcode is the address in?")
    if zipcode == '84041':
        DeliveryTime = 30
        DeliveryFee = 6.50
    else:
        DeliveryTime = 45
        DeliveryFee = 7.50
else:
    DeliveryFee = 0

暫無
暫無

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

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