python if elif else语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19371643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python if elif else statement
提问by sakefon
I'm trying to create a program with python that calculate the cost for shipping.
我正在尝试使用 python 创建一个程序来计算运输成本。
However, I can't run the program to where it works properly.
但是,我无法将程序运行到正常运行的位置。
What ever my total is the same amount comes out as $6 for US and $8 for Canada. I can't seem to get pass that.
我的总金额与美国的 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 .00"
elif total <= "100":
print "Shipping Costs .00"
elif total <= "150":
print "Shipping Costs .00"
else:
print "FREE"
if country == "Canada":
if total <= "50":
print "Shipping Costs .00"
elif total <= "100":
print "Shipping Costs .00"
elif total <= "150":
print "Shipping Costs .00"
else:
print "FREE"
采纳答案by Curry
- you should get integer from raw_input, not string. use
int()
. - comparison values like 50, 100, 150, ... also should be
integer
.
- 你应该从 raw_input 得到整数,而不是字符串。使用
int()
. - 像 50, 100, 150, ... 这样的比较值也应该是
integer
.
below is fixed code.
下面是固定代码。
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 .00"
elif total <= 100:
print "Shipping Costs .00" # improved indentation
elif total <= 150:
print "Shipping Costs .00" # improved indentation
else:
print "FREE"
if country == "Canada":
if total <= 50:
print "Shipping Costs .00"
elif total <= 100:
print "Shipping Costs .00"
elif total <= 150:
print "Shipping Costs .00"
else:
print "FREE"
回答by Jeanne Boyarsky
You can't compare Strings numerically. Instead convert to an int first and then compare.
您不能以数字方式比较字符串。而是先转换为 int 然后比较。
For example:
例如:
if int(total) < 50
Variables to avoid duplication would help too.
避免重复的变量也会有所帮助。
回答by Lie Ryan
This:
这个:
total = raw_input('What is the total amount for your online shopping?')
produces a string. Comparison between string and numbers are not very well defined. You need to convert total to a number first. Example:
产生一个字符串。字符串和数字之间的比较没有很好地定义。您需要先将总数转换为数字。例子:
total = int(raw_input('What is the total amount for your online shopping?'))
(this ignores input error handling such as when the user's input is not a number)
(这会忽略输入错误处理,例如当用户输入不是数字时)
Note that the behavior changes in Python 2.x and Python 3.x. In Python 2.x:
请注意,Python 2.x 和 Python 3.x 中的行为发生了变化。在Python 2.x 中:
Objects of different types, except different numeric types and different string types, never compare equal; such objects are ordered consistently but arbitrarily (so that sorting a heterogeneous array yields a consistent result).
...
CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don't support proper comparison are ordered by their address.
不同类型的对象,除了不同的数值类型和不同的字符串类型,从不比较相等;这些对象的排序是一致但任意的(因此对异构数组进行排序会产生一致的结果)。
...
CPython实现细节:除数字外的不同类型的对象按类型名称排序;不支持正确比较的相同类型的对象按其地址排序。
while in Python 3.x:
Objects of different types, except different numeric types, never compare equal.
不同类型的对象,除了不同的数字类型,永远不会比较相等。
回答by aIKid
You are comparing stringsnumerically. That's impossible, like comparing apple
with orange
, which one is bigger? The computer won't understand that, it needs to compare the size.
您正在以数字方式比较字符串。那是不可能的,比较喜欢apple
用orange
,哪一个是更大?计算机不会理解,它需要比较大小。
To do that, we need to convert it to an integer. Use the int()
function. Here:
为此,我们需要将其转换为整数。使用该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 .00"
elif total <= 100:
print "Shipping Costs .00"
elif total <= 150:
print "Shipping Costs .00"
else:
print "FREE"
if country == "Canada":
if total <= 50:
print "Shipping Costs .00"
elif total <= 100:
print "Shipping Costs .00"
elif total <= 150:
print "Shipping Costs .00"
else:
print "FREE"
Hope this helps!
希望这可以帮助!
回答by Trevor Merrifield
When you compare strings it does so lexicographically, like in a phone book. For example:
当您比较字符串时,它会按字典顺序进行比较,就像在电话簿中一样。例如:
"a" < "b"
: True"bill" < "bob"
: True"100" < "3"
: True
"a" < "b"
: 真"bill" < "bob"
: 真"100" < "3"
: 真
If you want to compare numbers in the order that we count them you need to use the int type.
如果你想按照我们计数的顺序比较数字,你需要使用 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?'))
Then change all of the string literals in your code like "50"
to integer literals like 50
.
然后将代码中的所有字符串文字更改"50"
为整数文字,如50
.
回答by 11swallowedinthesea
It's like adding apples & houses to get the total which is impossible. It needs to be the same type, in this case integer type, to get the total. Use the int() to convert the string into an integer.
这就像添加苹果和房屋来获得总数,这是不可能的。它需要是相同的类型,在这种情况下是整数类型,以获得总数。使用 int() 将字符串转换为整数。
total = int(raw_input('What is the total amount for your online shopping?'))
could also be (but less preferable):
也可以(但不太可取):
total = raw_input('What is the total amount for your online shopping?')
total = int(total)
回答by CasperTN
When using raw_input your user input comes in as a string and you cannot calculate numbers in the format of strings. So you need to change your string input to an integer in order to make the comparisons. You can do like this:
使用 raw_input 时,您的用户输入以字符串形式出现,您无法以字符串格式计算数字。因此,您需要将字符串输入更改为整数才能进行比较。你可以这样做:
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 .00"
elif total <= 100:
print "Shipping Costs .00"
elif total <= 150:
print "Shipping Costs .00"
else:
print "FREE"
elif country == "Canada":
if total <= 50:
print "Shipping Costs .00"
elif total <= 100:
print "Shipping Costs .00"
elif total <= 150:
print "Shipping Costs .00"
else:
print "FREE"
else:
print "Try Again"
回答by Biddrup Mallick
I am just a fresher here and python programming. I was trying to solve your problem. Hope, this one could help you.
我只是这里的新手和 python 编程。我试图解决你的问题。希望,这个可以帮到你。
if country == 'US':
if total <= 50:
print ('Shipping Costs .00')
elif total <= 100:
print ('Shipping Costs .00')
elif total <= 150:
print ('Shipping Costs .00')
else:
print ('FREE')
elif country == 'Canada':
if total <= 50:
print ('Shipping Costs .00')
elif total <= 100:
print ('Shipping Costs .00')
elif total <= 150:
print ('Shipping Costs .00')
else:
print ('FREE')
else:
print ('Country name is case sensetive so do it perfectly')
回答by Jude Eliboh
input returns a string
输入返回一个字符串
if total is supposed to return an input for math operations then you should float the input
如果 total 应该返回数学运算的输入,那么您应该浮动输入
total = (raw_input('What is the total amount for your online shopping?')) total = float(total)
total = (raw_input('你网上购物的总金额是多少?')) total = float(total)