【问题标题】:Python code program using FOR loop not working使用 FOR 循环的 Python 代码程序不起作用
【发布时间】:2020-04-09 04:16:15
【问题描述】:

我需要编写一个使用 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 语句是否应该在循环中缩进?
  • 我猜这是真正的缩进,为什么只添加最后一个deposit 是有道理的。

标签: python python-3.x loops coding-style accumulator


【解决方案1】:

在这种情况下,我建议使用 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

【讨论】:

    【解决方案2】:

    请检查你的缩进,你的 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))
    

    【讨论】:

      【解决方案3】:

      在您的代码的当前版本中,您的所有 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
      

      【讨论】:

        猜你喜欢
        • 2015-07-04
        • 2018-09-09
        • 2022-12-10
        • 1970-01-01
        • 2016-09-02
        • 2021-05-04
        • 1970-01-01
        • 1970-01-01
        • 2018-10-12
        相关资源
        最近更新 更多