python if-elif-else语句
我正试图用python创建一个程序来计算运输成本 但是,我无法将程序运行到正常工作的位置 我的总数是一样的,美国是6美元,加拿大是8美元。我好像没法通过那个python if-elif-else语句,python,if-statement,nested,Python,If Statement,Nested,我正试图用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 <= "5
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"
total=raw_input('您网上购物的总金额是多少?')
国家=原始输入(“在美国或加拿大境内运输”)
如果国家=“美国”:
如果total则无法对字符串进行数字比较。而是先转换为int,然后进行比较
例如:
if int(total) < 50
如果整数(总计)<50
避免重复的变量也会有所帮助。这:
total = raw_input('What is the total amount for your online shopping?')
生成一个字符串。字符串和数字之间的比较没有很好的定义。您需要先将总数转换为一个数字。例如:
total = int(raw_input('What is the total amount for your online shopping?'))
(这将忽略输入错误处理,例如当用户的输入不是数字时)
请注意,Python2.x和Python3.x中的行为发生了变化。在:
不同类型的对象,除了不同的数字类型和不同的字符串类型外,从不进行相等比较;这些对象的排序是一致的,但却是任意的(因此,对异构数组进行排序会产生一致的结果)
CPython实现细节:除数字外,不同类型的对象按其类型名排序;不支持正确比较的相同类型的对象按其地址排序
在职期间:
不同类型的对象(不同的数值类型除外)从不进行相等比较
您正在对字符串进行数值比较。这是不可能的,比如比较苹果和橙色,哪个更大?计算机无法理解这一点,它需要比较大小
为此,我们需要将其转换为整数。使用int()
函数。在这里:
#convert it to an integer straight away
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"
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"
#直接将其转换为整数
total=int(原始输入(“您的网上购物总额是多少?”)
国家=原始输入(“在美国或加拿大境内运输”)
如果国家=“美国”:
如果总计
您应该从原始输入中获取整数,而不是字符串。使用int()
比较值,如50、100、150。。。也应该是整数
下面是固定代码
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"
total=int(原始输入('网上购物的总金额是多少?'))
国家=原始输入(“在美国或加拿大境内运输”)
如果国家=“美国”:
如果total当你比较字符串时,它是按字典顺序进行比较的,就像在电话簿中一样。例如:
“a”<“b”
:True
“bill”
:True
“100”<“3”
:真
如果您想按照我们计数的顺序比较数字,您需要使用int类型
total=int(原始输入(“网上购物的总金额是多少?”)
然后将代码中的所有字符串文字(如“50”
)更改为整数文字(如50
)这就像添加苹果和房子以获得总数一样,这是不可能的。它需要是相同的类型,在本例中是整数类型,才能得到总数。使用int()将字符串转换为整数
total = int(raw_input('What is the total amount for your online shopping?'))
也可能是(但不太可取):
当使用原始输入时,您的用户输入以字符串的形式输入,您不能以字符串的形式计算数字。因此,您需要将字符串输入更改为整数,以便进行比较。
您可以这样做:
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"
elif total <= 150:
print "Shipping Costs $12.00"
else:
print "FREE"
elif 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"
else:
print "Try Again"
total=int(原始输入('网上购物的总金额是多少?'))
国家=原始输入(“在美国或加拿大境内运输”)
如果国家=“美国”:
如果total我只是python编程的新手。我想解决你的问题。希望这个能帮助你
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')
elif 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')
else:
print ('Country name is case sensetive so do it perfectly')
如果国家==“美国”:
如果total输入返回一个字符串
如果total应该为数学运算返回一个输入,那么您应该浮动该输入
总计=(原始输入(“您的网上购物总额是多少?”)
总计=浮动(总计)
删除if语句中整数的引号,例如:
如果总计如果总计,您需要正确格式化代码。。。尝试将其缩进4个空格。如果购买金额超过150美元,是否有意免费送货int(“5”)
似乎是拼写5
的一种冗长方式。是的。但在实际代码中,total是一个变量,因此需要int(total)。啊,你的意思是,如果int(total)<50,它应该是。为了清晰起见,我将进行编辑。我实际上认为将total
转换为int
(或者,IMHO,一个float
)一次会更有意义,而不是重复每次比较的转换。我也是。因此,我对变量的注释是为了避免重复。total=int(原始输入(“网上购物的总金额是多少?”)并将几个字符串更改为插入问题和解决方案的实际描述将比只发布代码要好得多。@Curry非常感谢!指出您所做的更改。如果是Python,请将缩进设置正确,否则程序将因此出错。此外,在这种情况下,您只更改了print
根据python版本的不同,这可能会有所帮助,但并不是问题的答案。看起来这个答案已经得到了回答。如果你有什么要补充的,请相应地编辑你的帖子。
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')
elif 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')
else:
print ('Country name is case sensetive so do it perfectly')