我是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
Run Code Online (Sandbox Code Playgroud)
你需要:
total = total + (q*1.3)
Run Code Online (Sandbox Code Playgroud)
要么:
total += (q*1.3)
Run Code Online (Sandbox Code Playgroud)
整数是不可变的,只是做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
Run Code Online (Sandbox Code Playgroud)