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

2024-09-27 22:32:12 发布

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

我需要编写一个程序使用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))

Tags: and用户youifhavetotalprint存款
3条回答

请检查缩进,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语句都在for循环之外,这意味着它们将只针对最后一个用户输入(discount)运行,因为这将是循环完成执行后持续存在的discount

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

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 range(7)循环内。您还需要检查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

然后,输出如您所期望的:

Please enter your deposit amount: 10001
Please enter your deposit amount: 501
Please enter your deposit amount: 101
Please enter your deposit amount: 11
Please enter your deposit amount: 2
Please enter your deposit amount: 3
Please enter your deposit amount: 4
You have 1 deposit over  or equal to 1000 dollars.
You have 1 deposit between 500 and 999 dollars.
You have 1 deposit between 100 and 499 dollars. 
You have 4 deposit below 100 dollars.
Your balance is : 10623

相关问题 更多 >

    热门问题