简体   繁体   English

如何找出循环中的输出数量?

[英]How can I find out the number of outputs in a loop?

I am a beginner at python and I'm struggling with one of my (simple) college assignments.我是 python 的初学者,我正在为我的一项(简单的)大学作业而苦苦挣扎。 I have been given the following instructions:我得到了以下指示:

A bank is offering a savings account where a yearly fee is charged.一家银行提供一个储蓄账户,每年收取费用。 Write a program that lets the user enter编写一个程序,让用户输入

  1. An initial investment.初始投资。

  2. The yearly interest rate in percent.以百分比表示的年利率。

  3. The yearly fee.年费。

the program should then calculate the time it takes for the investment to double.然后,程序应计算投资翻倍所需的时间。 The interest is added on once per year.利息每年增加一次。

An example run of the program:程序运行示例:

Enter the investment: 1000输入投资:1000

Enter the interest rate: 10输入利率:10

Enter the fee: 10输入费用:10

The investment doubles after 7 years. 7 年后,投资翻了一番。

I have formulated the following code but am receiving an error message with regards to t.我已经制定了以下代码,但我收到一条关于 t 的错误消息。 I would really appreciate if I could get some help, thanks!:如果我能得到一些帮助,我将不胜感激,谢谢!:

t=0
p=float(input("Enter the investment:"))

a=float(input("Enter the interest rate:"))

m=float(input("Enter the fee:"))

i=(float(a/100))
f=p
while f<=(2*p):
    f=(float(f*((1+i)**t)-m)
    t=t+1

print("The investment doubles after",t,"years")

I tried to write this in a way that was very easy to follow and understand.我试图以一种非常容易理解和理解的方式来写这篇文章。 I edited it with comments to explain what is happening line by line.我用注释对其进行了编辑,以逐行解释正在发生的事情。 I would recommend using more descriptive variables.我建议使用更多的描述性变量。 t/p/a/m/f may make a lot of sense to you, but going back to this program 6 months from now, you may have issues trying to understand what you were trying to accomplish. t/p/a/m/f 对你来说可能很有意义,但是从现在起 6 个月后回到这个程序,你可能会在试图理解你想要完成的事情时遇到问题。 NOTE You should use input instead of raw_input in my example if using Python 3+.注意如果使用 Python 3+,您应该在我的示例中使用 input 而不是 raw_input 。 I use 2.7 so I use raw_input.我使用 2.7,所以我使用 raw_input。

#first we define our main function
def main():
    #investment is a variable equal to user input. it is converted to a float so that the user may enter numbers with decimal places for cents
    investment = float(raw_input("Starting Investment:  "))
    #interest is the variable for interest rate. it is entered as a percentage so 5.5 would equate to 5.5%
    interest = float(raw_input("Interest Rate as %, ex: 5.5  "))
    #annual_fee is a variable that will hold the value for the annual fee.
    annual_fee = float(raw_input("Annual Fee:  "))
    #years is a variable that we will use with a while loop, adding 1 to each year (but we wait until within the loop to do this)
    years = 1
    #we use a while loop as opposed to a for loop because we do not know how many times we will have to iterate through this loop to achieve a result. while true is always true, so this segment is going to run without conditions
    while True:
        #this is a variable that holds the value of our total money per year, this is equal to the initial investment + investment * interest percentage - our annual fee per year
        #I actually had to try a few different things to get this to work, a regular expression may have been more suited to achieve an interest % that would be easier to work with. do some research on regular expressions in python as you will sooner or later need it.
        total_per_year = investment + (years * (investment * (interest / 100))) - (annual_fee * years)
        #now we start adding 1 to our years variable, since this is a while loop, this will recalculate the value of total_per_year variable
        years += 1
        #the conditional statement for when our total_per_year becomes equal to double our initial investment
        if total_per_year >= 2 * investment:
            #print years value (at time condition is met, so it will be 5 if it takes 5 years) and the string ' Years to Double Investment'
            print years,' Years to Double Investment'
            #prints 'You will have $' string and then the value our variable total_per_year
            print 'You will have $', total_per_year
            #this will break our while loop so that it does not run endlessly
            break
        #here is error handling for if the fee is larger than investment + interest
        if (years * annual_fee) >= (years * (investment * (interest / 100))):
            print('Annual Fee Exceeds Interest - Losing Money')
            break
#initial call of our main function/begins loop
main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM