[英]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.