none - output

Ingo Linkweiler i.linkweiler at web.de
Sat Nov 9 13:27:51 EST 2002


> schuld = 100000.0
> zinsfaktor = 5.0
> tilgung = 20000.0
 > def tilgungsplan(schuld, zinsfaktor, tilgung):
 >   print "Periode Restschuld Zinsen Tilgung"
 >   for i in range(0,5):
 >     zinsen = (schuld*zinsfaktor)/100
 >     echtetilgung=tilgung-zinsen
 >     i = i + 1
 >     schuld = schuld - echtetilgung
 >     print i," "," ", schuld," ", zinsen," ", echtetilgung
 >
 > print tilgungsplan(schuld, zinsfaktor, tilgung)
 > print "Restzahlung: ", schuld

> This produces this output:
> 
> Periode Restschuld Zinsen Tilgung
> > 85000.0 5000.0 15000.0
> 2 69250.0 4250.0 15750.0
> 3 52712.5 3462.5 16537.5
> 4 35348.125 2635.625 17364.375
> 5 17115.53125 1767.40625 18232.59375
> None
> Restzahlung: 100000.0
> 
> My problem is that the "None" (I do not know how to avoid this output!) and
> the value for "Restzahlung" (how much is still left to pay) is not correct!
> > May you help me on this on?

1. bug)
You do:
print tilgungsplan(schuld, zinsfaktor, tilgung)

the function "tilgungsplan" has no return value, so the output is none.
A function can return a value, then you must use a "return variable" 
statement.

2. bug)
all parameters given to a function are LOCAL. This meens, if you change 
the variables given as parameters within a function, the changes are 
lost when the function exits. So you get "Restzahlung: 100000.0"

3. bug)
NEVER change the value of the for-loop! The For-Loop does this for you 
automatically. If you want to count yourself, use a WHILE-loop.


Try this:

def tilgungsplan(schuld, zinsfaktor, tilgung):
   print "Periode Restschuld Zinsen Tilgung"
   for i in range(1,6):
     zinsen = (schuld*zinsfaktor)/100
     echtetilgung=tilgung-zinsen
     schuld = schuld - echtetilgung
     print i," "," ", schuld," ", zinsen," ", echtetilgung
   return schuld

restzahlung = tilgungsplan(schuld, zinsfaktor, tilgung)
print "Restzahlung: ", restzahlung

Viel Erfolg!

Ingo




More information about the Python-list mailing list