【问题标题】:Setting a variable counter to start at zero but doesn't将变量计数器设置为从零开始,但不是
【发布时间】:2017-03-23 18:55:09
【问题描述】:

我正在用更新的代码重新发布这个问题。

当我运行此代码来计算多年来获得的单利时(以漂亮的表格形式),我的表格列从 5 开始。此外,总数是从 5 开始计算的。只是为了澄清,当询问 totYear 时,我使用 5 作为用户输入的示例。

    #Declare the necessary variables.
princ = 0
interest = 0.0
totYear = 0
year = 1

#Get the amont of principal invested.
print("Enter the principal amount.")
princ = int(input())

#Get the interest rate being applied.
print("Enter the interest rate.")
interest = float(input())

#Get the total amount of years principal is invested.
print ("Enter the total number of years you're investing this amonut.")
totYear = int(input())

print("Year      Interest")
for year in range(totYear):
    total=totYear*interest*princ
    print (totYear,"      $",total)
    totYear+=1

if total<100:
    print("That is not too much interest!")
else:
    print("This interest really adds up!")

这是输出画面:

Enter the principal amount.
10
Enter the interest rate.
5
Enter the total number of years you're investing this amonut.
5
Year      Interest
5       $ 250.0
6       $ 300.0
7       $ 350.0
8       $ 400.0
9       $ 450.0
This interest really adds up!

感谢您的帮助!

【问题讨论】:

  • 您想打印year,因为这是将通过该范围的变量。我认为您不需要增加 totYear
  • 尝试打印year而不是totYear

标签: python python-3.x for-loop logical-operators


【解决方案1】:

这是你想要的输出吗?

Enter the principal amount.
10
Enter the interest rate.
5
Enter the total number of years you're investing this amonut.
5
Year      Interest
0       $ 250.0
1       $ 300.0
2       $ 350.0
3       $ 400.0
4       $ 450.0
This interest really adds up!

如果是这样,那么您需要将 print (totYear," $",total) 替换为:

print (year," $",total)

另外,你不需要初始化year,因为它是在for中初始化的。

【讨论】:

    【解决方案2】:

    首先,您需要将循环中的 totYear 更改为 year,因为 year 是计数器

    for year in range(totYear):
        total=year*interest*princ
        print (year,"      $",total)
        #totYear+=1 //delete this line
    

    然后删除最后一个增量

    【讨论】:

    • 谢谢,这有助于我计算每次迭代的同一年(totYear)。现在它通过将每年都纳入计算来做到这一点。如何让输出包括最多并包括 totYear?我想展示他们在最后一年累积的利息,但之前已经中断了一次。
    • 然后将 for 循环更改为 for year in range(totYear+1):如果有帮助,请点赞并接受答案,谢谢。
    • 我使用的修复是在声明 totYear 变量时向用户输入添加“+1”。由于 range 函数只读取从起始值到结束值,但不包括结束值。
    • 这不是那么有利,因为如果您打算在其他地方使用该 totYear 变量可能会给您带来错误,循环范围内的 +1 是一个更好的解决方案。
    • 如果有帮助,请点击答案左侧的向上箭头和打勾,感谢他们的时间和精力:)
    【解决方案3】:

    这是我在上一个问题中给你的循环,更新了打印语句以包含年份。我已经添加了您已经知道的“+1”调整。随意格式化。

    princ = 10000
    interest = 0.10
    totYears = 5
    
    for year in range(1, totYears+1):
        total = year * interest * princ
        print (year, total)
    

    输出:

    1 1000.0
    2 2000.0
    3 3000.0
    4 4000.0
    5 5000.0
    

    【讨论】:

      【解决方案4】:

      我相信您已将 totYear 设置为等于 int(input()) ,这就是计数器从用户输入的年数开始的原因。

      您可能想要创建另一个等于 int(input()) 的变量 numYears,并使用 range(numYears) 而不是 range(totYear)。

      #variable that gets number of years user inputs
      numYears = 0; 
      #Get the total amount of years principal is invested.
      print ("Enter the total number of years you're investing this amonut.")
      numYears = int(input())
      
      print("Year      Interest")
      for year in range(numYears):
      total=totYear*interest*princ
      print (totYear,"      $",total)
      totYear+=1
      

      【讨论】:

        猜你喜欢
        • 2022-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-20
        • 1970-01-01
        • 2012-11-27
        • 2012-11-11
        • 2021-10-10
        相关资源
        最近更新 更多