2016-10-14 7 views
0

나는 수업료의 미래 가치를 계산할 프로그램을 작성하려고합니다. 수업료는 오늘 $ 10000이며, 매년 5 % 씩 인상됩니다. 저는 제 10 학년 수업료와 함께 10-13 학년을 합친 총 수업료를 알려주는 프로그램을 작성하려고합니다.첫 번째에 기초한 두 번째 for 루프 시작하기

나는 루프를 위해 2를 쓰는 것으로 바른 길을 가고 있지만 프로그램이 실행되지 않을 것이라고 거의 확신한다.

def tuition(): 

    tuition_cost=10000 
    increase=1.05 
    running_total=0 

#first loop includes tuition for years 1-10 
#update tuition for year 10 
    for year in range (1,11,1): 
     tuition_cost=((tuition_cost*(increase**year)) 

    print(tuition_cost) 

    for year in range (10,14,1): 
     tuition_cost=(tuition_cost*(increase**year)) 
     running_total=running_total+tuition_cost 

    print(running_total) 

tuition() 

누구에게 의견이 있습니까?

+0

실행되지 않습니다? 아무것도하지 않습니까? 그것은 약간의 오류를 제기합니까? –

+0

괄호'tuition_cost = ((tuition_cost * (증가 ** 년))' –

+0

첫 번째 forloop에 여분의 괄호가 있기 때문에 처음에는 구문 오류가 발생했습니다. 문제가 해결되면 실행되지만 올바르지 않습니다. 14 만 가지의 결과와 275,000 가지의 결과가 있습니다. 답변은 약 16000 달러와 70000 달러 여야합니다. –

답변

0

는 시도이 하나

def tuition(): 
    tuition_cost=10000 
    increase=1.05 

    print('the cost for the year 10:', tuition_cost*(increase**10)) 

    running_total = 0 
    for year in range(10): 
     running_total += tuition_cost*(increase**year) 

    print('the cost for 10 years:', running_total) 

    for year in range(10,14,1): 
     running_total += tuition_cost*(increase**year) 

    print('the cost for 14 years:', running_total) 

tuition() 
+0

데프 수업료() : \t tuition_cost = 10000 \t 증가 = 1.05 \t running_total = 0 #first 루프 포함 년간 수업료 년 1-10 \t #UPDATE 수업료 10 \t 범위 년 (-1,11,11-)에 대한 : \t \t 수업료 = (tuition_cost의 * (증가 ** 년)) \t \t 인쇄 (혀를 내다 이온) 범위의 연도 \t (10,14,1) \t \t 학비 = (학비 * (증가 ** 년)) \t \t running_total = running_total + 학비 \t 인쇄 (running_total) \t 수업료() –

0

내가, 당신의 프로그램은 다음과 같이해야한다고 생각 :

tuition_cost = 10000 
increase = 1.05 
running_total = 0 

for year in range(0, 11): 
    price_for_year = tuition_cost*(increase**year) 
    print(price_for_year) 

for year in range(10, 14): 
    running_total += price_for_year 
    price_for_year = tuition_cost*(increase**year) 
    print(running_total) 
관련 문제