使用“for”循环的Python数学运算不起作用

时间:2017-09-11 18:28:42

标签: python for-loop if-statement

我正在尝试建立一个非常基本的帐户减息系统 我正在使用的代码询问用户是否愿意购买价值200美元的物品。这个想法是200美元将从名为total的变量中扣除,但由于某种原因,这不起作用。

我不知道是否可以在if语句中嵌套For循环。 谢谢你的时间

total = 1500
print('Would you like to buy this item?')
print('It costs 100 bucks')
purchaseConfirm = input()
if purchaseConfirm == 'yes':
    for num in range(200):
        total = total-1   # Why is this thing not functioning???
        print(total)
        break

2 个答案:

答案 0 :(得分:5)

我认为你根本不需要for循环......

total = 1500
print('Would you like to buy this item?')
print('It costs 200 bucks')
purchaseConfirm = input()
cost = 200
if purchaseConfirm == 'yes':
  total = total - cost
  print(total)

答案 1 :(得分:2)

这将有效:

total = 1500
print('Would you like to buy this item?')
print('It costs 100 bucks')
purchaseConfirm = raw_input()
if purchaseConfirm == 'yes':
    for num in range(200):
        total = total-1   # Why is this thing not functioning???
        print(total)

你应该删除break以保持直到循环...