使用FOR循环的Python代码程序不工作

使用FOR循环的Python代码程序不工作,python,python-3.x,loops,coding-style,accumulator,Python,Python 3.x,Loops,Coding Style,Accumulator,我需要编写一个程序使用FOR循环,要求用户7存款。当用户输入存款金额时,需要使用累加概念更新余额 此外,还应记录超过或等于1000美元、500至999美元、100至499美元以及0至99美元的存款数量。 输出=显示上述各组的计数以及所有存款的最终余额 问题:最后一个输入号码存款是唯一一个正在注册的存款 amount1000orover = 0 amount500to999 = 0 amount100to499 = 0 less99 = 0 total = 0 for count in rang

我需要编写一个程序使用FOR循环,要求用户7存款。当用户输入存款金额时,需要使用累加概念更新余额

此外,还应记录超过或等于1000美元、500至999美元、100至499美元以及0至99美元的存款数量。 输出=显示上述各组的计数以及所有存款的最终余额

问题:最后一个输入号码存款是唯一一个正在注册的存款

amount1000orover = 0
amount500to999 = 0
amount100to499 = 0
less99 = 0
total = 0

for count in range(7):
 deposit = int(input("Please enter your deposit amount: "))

if deposit >= 1000:
      amount1000orover + 1
      total=total + deposit

if deposit>=500 and deposit<=999:
     amount500to999 = amount500to999 + 1
     total=total + deposit

if deposit>= 100 and deposit<=499:
     amount100to499 = amount100to499 + 1
     total=total + deposit

if deposit>=0 and deposit<=99:
     less99 = less99 + 1
     total=total + deposit

print("You have "+str(amount1000orover)+" deposit over  or equal to 1000 dollars.")
print("You have "+str(amount500to999)+" deposit between 500 and 999 dollars.")
print("You have "+str(amount100to499)+" deposit between 100 and 499 dollars. ")
print("You have "+str(less99)+" deposit below 100 dollars.")
print("Your balance is : "+str(total))

在当前版本的代码中,所有if语句都在for循环之外,这意味着它们将仅在最后一次用户输入折扣时运行,因为这将是在循环完成执行后持续存在的折扣值

您可以通过一个示例看到这一点:

Please enter your deposit amount: 1
Please enter your deposit amount: 2
Please enter your deposit amount: 3
Please enter your deposit amount: 4
Please enter your deposit amount: 5
Please enter your deposit amount: 6
Please enter your deposit amount: 7
You have 0 deposit over  or equal to 1000 dollars.
You have 0 deposit between 500 and 999 dollars.
You have 0 deposit between 100 and 499 dollars. 
You have 1 deposit below 100 dollars.
Your balance is : 7
相反,您需要缩进if语句,使它们位于for count in range7循环内。您还需要检查if语句的内容,因为它们不会像您期望的那样增加amount1000orover计数器。您可以使用+=运算符来简化这些操作,即:

for count in range(7):                                                          
    deposit = int(input("Please enter your deposit amount: "))                  
                                                                                
    if deposit >= 1000:                                                         
        amount1000orover += 1                                                  
        total=total + deposit                                                   
                                                                                
    if deposit >= 500 and deposit <= 999:                                           
        amount500to999 += 1                                                     
        total += deposit                                                        
                                                                                
    if deposit >= 100 and deposit <= 499:                                          
        amount100to499 += 1                                                     
        total += deposit                                                        
                                                                                
    if deposit >= 0 and deposit <= 99:                                              
        less99 += 1                                                             
        total += deposit

请检查缩进,if循环应该在for循环内部,而不是外部。应该是这样的

amount1000orover = 0
amount500to999 = 0
amount100to499 = 0
less99 = 0
total = 0

for count in range(7):
   deposit = int(input("Please enter your deposit amount: "))

   if deposit >= 1000:
       amount1000orover += 1
       total=total + deposit

   if deposit>=500 and deposit<=999:
       amount500to999 = amount500to999 + 1
       total=total + deposit

   if deposit>= 100 and deposit<=499:
       amount100to499 = amount100to499 + 1
       total=total + deposit

   if deposit>=0 and deposit<=99:
       less99 = less99 + 1
       total=total + deposit

print("You have "+str(amount1000orover)+" deposit over  or equal to 1000 dollars.")
print("You have "+str(amount500to999)+" deposit between 500 and 999 dollars.")
print("You have "+str(amount100to499)+" deposit between 100 and 499 dollars. ")
print("You have "+str(less99)+" deposit below 100 dollars.")
print("Your balance is : "+str(total))

在这种情况下,我建议使用elif语句来节省大量的输入。此外,由于这些钱被划分为不同的范围,你会损失特定的存款金额,尽管你仍然拥有总金额。解决问题的另一种方法是:

_1000_plus = 0
_500_to_999 = 0
_100_to_499 = 0
under_99 = 0
deposits = []  # if you keep a list of deposits then you won't lose that data
for i in range(7):
    deposit = float(input("Please enter your deposit amount: "))  # use float because the user might try to enter cents
    deposits.append(deposit)
    if deposit < 100:
        under_99 += 1
    elif deposit < 500:  # elif is what you want to use. It saves lots of typing
        _100_to_499 += 1
    elif deposit < 1000:
        _500_to_999 += 1
    else:
        _1000_plus += 1

print(f"You have {_1000_plus} deposit over or equal to 1000 dollars.")
print(f"You have {_500_to_999} deposit between 500 and 999 dollars.")
print(f"You have {_100_to_499} deposit between 100 and 499 dollars. ")
print(f"You have {under_99} deposit below 100 dollars.")
print(f"Your balance is : ${sum(deposits):.2f}")  # writes sum of deposits with 2 decimal points

这真的是你缩进的样子吗?那些if语句是否应该在循环中缩进?我想这是真正的缩进,这对于为什么只添加最后的存款是有意义的。
_1000_plus = 0
_500_to_999 = 0
_100_to_499 = 0
under_99 = 0
deposits = []  # if you keep a list of deposits then you won't lose that data
for i in range(7):
    deposit = float(input("Please enter your deposit amount: "))  # use float because the user might try to enter cents
    deposits.append(deposit)
    if deposit < 100:
        under_99 += 1
    elif deposit < 500:  # elif is what you want to use. It saves lots of typing
        _100_to_499 += 1
    elif deposit < 1000:
        _500_to_999 += 1
    else:
        _1000_plus += 1

print(f"You have {_1000_plus} deposit over or equal to 1000 dollars.")
print(f"You have {_500_to_999} deposit between 500 and 999 dollars.")
print(f"You have {_100_to_499} deposit between 100 and 499 dollars. ")
print(f"You have {under_99} deposit below 100 dollars.")
print(f"Your balance is : ${sum(deposits):.2f}")  # writes sum of deposits with 2 decimal points