使用“for”循环的Python数学运算无法正常工作

2024-06-26 15:02:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试建立一个非常基本的帐户货币减少系统 我使用的代码询问用户是否愿意购买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

Tags: 对象代码用户forif系统时间货币
2条回答

我认为你根本不需要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)

这将起作用:

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,直到循环的and。。。你知道吗

相关问题 更多 >