22

我正在尝试使用 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"
4

10 回答 10

23
  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"
于 2013-10-15T01:00:35.703 回答
12

您无法以数字方式比较字符串。而是先转换为 int 然后比较。

例如:

if int(total) < 50

避免重复的变量也会有所帮助。

于 2013-10-15T00:59:06.247 回答
7

您正在以数字方式比较字符串。那是不可能的,就像与 相比appleorange哪个更大?计算机不会理解,它需要比较大小

为此,我们需要将其转换为整数。使用该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"

希望这可以帮助!

于 2013-10-15T01:00:16.417 回答
7

当您比较字符串时,它会按字典顺序进行,就像在电话簿中一样。例如:

"a" < "b": 对
"bill" < "bob": 对
"100" < "3": 对

如果你想按照我们计算它们的顺序比较数字,你需要使用 int 类型。

total = int(raw_input('What is the total amount for your online shopping?'))

然后将代码中的所有字符串文字更改"50"为整数文字,例如50.

于 2013-10-15T01:10:59.067 回答
4

这个:

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?'))

(这会忽略输入错误处理,例如当用户输入不是数字时)

请注意,Python 2.x 和 Python 3.x 中的行为发生了变化。在Python 2.x中:

不同类型的对象,除了不同的数值类型和不同的字符串类型,从不比较相等;此类对象的排序一致但任意(因此对异构数组进行排序会产生一致的结果)。

...

CPython 实现细节:除数字外的不同类型的对象按其类型名称排序;不支持正确比较的相同类型的对象按其地址排序。

Python 3.x中:

不同类型的对象,除了不同的数值类型,永远不会比较相等。

于 2013-10-15T00:59:38.710 回答
4

使用 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 $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"
于 2016-06-19T09:37:37.027 回答
3

这就像添加苹果和房屋来获得总数,这是不可能的。它需要是相同的类型,在这种情况下为整数类型,才能获得总数。使用 int() 将字符串转换为整数。

 total = int(raw_input('What is the total amount for your online shopping?'))

也可以是(但不太可取):

 total = raw_input('What is the total amount for your online shopping?')
 total = int(total)
于 2016-06-19T06:18:09.023 回答
2

输入返回一个字符串

如果 total 应该返回数学运算的输入,那么您应该浮动输入

total = (raw_input('你网购的总金额是多少?')) total = float(total)

于 2019-10-08T18:30:58.773 回答
1

从 if 语句中的整数中删除引号,例如:

如果总计 <= "50" -------> 如果总计 <= 50

于 2021-02-24T03:15:44.333 回答
-1

我只是这里的新手和 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')
于 2016-10-12T06:07:26.097 回答