[英]How can I find out the number of outputs in a loop?
我是 python 的初学者,我正在为我的一项(简单的)大学作业而苦苦挣扎。 我得到了以下指示:
一家银行提供一个储蓄账户,每年收取费用。 编写一个程序,让用户输入
初始投资。
以百分比表示的年利率。
年费。
然后,程序应计算投资翻倍所需的时间。 利息每年增加一次。
程序运行示例:
输入投资:1000
输入利率:10
输入费用:10
7 年后,投资翻了一番。
我已经制定了以下代码,但我收到一条关于 t 的错误消息。 如果我能得到一些帮助,我将不胜感激,谢谢!:
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")
我试图以一种非常容易理解和理解的方式来写这篇文章。 我用注释对其进行了编辑,以逐行解释正在发生的事情。 我建议使用更多的描述性变量。 t/p/a/m/f 对你来说可能很有意义,但是从现在起 6 个月后回到这个程序,你可能会在试图理解你想要完成的事情时遇到问题。 注意如果使用 Python 3+,您应该在我的示例中使用 input 而不是 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.