gpt4 book ai didi

python if elif else 语句

转载 作者:IT老高 更新时间:2023-10-28 20:50:33 26 4
gpt4 key购买 nike

我正在尝试使用 python 创建一个计算运输成本的程序。

但是,我无法将程序运行到正常运行的位置。

我的总金额相同,美国为 6 美元,加拿大为 8 美元。我似乎无法通过。

total = raw_input('What is the total amount for your online shopping?')
country = raw_input('Shipping within the US or Canada?')

if country == "US":
if total <= "50":
print "Shipping Costs $6.00"
elif total <= "100":
print "Shipping Costs $9.00"
elif total <= "150":
print "Shipping Costs $12.00"
else:
print "FREE"

if country == "Canada":
if total <= "50":
print "Shipping Costs $8.00"
elif total <= "100":
print "Shipping Costs $12.00"
elif total <= "150":
print "Shipping Costs $15.00"
else:
print "FREE"

最佳答案

  1. 你应该从 raw_input 获取整数,而不是字符串。使用 int().
  2. 50、100、150 等比较值也应该是 integer

以下是固定代码。

total = int(raw_input('What is the total amount for your online shopping?'))
country = raw_input('Shipping within the US or Canada?')

if country == "US":
if total <= 50:
print "Shipping Costs $6.00"
elif total <= 100:
print "Shipping Costs $9.00" # improved indentation
elif total <= 150:
print "Shipping Costs $12.00" # improved indentation
else:
print "FREE"

if country == "Canada":
if total <= 50:
print "Shipping Costs $8.00"
elif total <= 100:
print "Shipping Costs $12.00"
elif total <= 150:
print "Shipping Costs $15.00"
else:
print "FREE"

关于python if elif else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19371643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com