簡體   English   中英

如何找出循環中的輸出數量?

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

我是 python 的初學者,我正在為我的一項(簡單的)大學作業而苦苦掙扎。 我得到了以下指示:

一家銀行提供一個儲蓄賬戶,每年收取費用。 編寫一個程序,讓用戶輸入

  1. 初始投資。

  2. 以百分比表示的年利率。

  3. 年費。

然后,程序應計算投資翻倍所需的時間。 利息每年增加一次。

程序運行示例:

輸入投資: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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM