1

我对 Python 完全陌生,我的代码有问题,我正在使用 if in 函数来做一个蔬菜水果商的计算器,当输入三个苹果被购买时,总产量为 0 英镑应该是 3.90 英镑

total = 0


print "Welcome to the green grocers, what would you like?"
print "1. Apples"
print "2: Bananas"
print "3. Oranges"
print "4. Total"



fruit = raw_input("What would you like?")

if "1" in fruit:
   q = input("How many?")
   total + (q*1.3)
   fruit = raw_input("What would you like?")

if "2" in fruit:
   g = input("How many?")
   total + (g*1.5)
   fruit = raw_input("What would you like?")

if "3" in fruit:
   l = input("How many?")
   total = (l*1.6)
   fruit = raw_input("What would you like?")

   if "4" in fruit:
   print "Your total is £", total
4

2 回答 2

6

你需要:

total = total + (q*1.3)

或者:

total += (q*1.3)

整数是不可变的,只是做total + (q*1.3)不会影响total,它只是返回一个新的整数。

>>> x = 1
>>> x + 2   # Simply returns a new value, doesn't affects `x`
3
>>> x       # `x` is still unchanged
1
>>> x += 1  # Assign the new value back to `x`
>>> x       # `x` is now updated.
2
于 2013-11-07T21:56:24.943 回答
3

您正在执行计算但未更新total变量。像这样做:

total = total + (g*1.5)

或与+=操作员:

total += g*15

希望这可以帮助!

于 2013-11-07T21:58:09.493 回答