None???

N

News

I just started using python a few weeks ago. This is my first program. You
input your income and what percent you want to save then it tells you how
much to put away. The the code works, but it puts in the word "none" after
it calls the function. Is the call sytax incorrect? I'd appreciate any
feedback you could offer .

# savings.py 09-14-03

print "Hello, welcome to my savings program."
print
print "I will ask you a few questions then calculate"
print "the info and give you a plan of action."
print
first_name = raw_input("What is your first name? ")
print
last_name = raw_input("What is your last name? ")
print
income = input("How much do you make a year after taxes? ")
print
percent = input("What percent of your income do you want to put aside? ")
print

weekly_income = income/52

def compute(income, percent):
result = income*percent/100
print result

print "Ok,",first_name + " " + last_name
print
print "This is how much you should put away a week:"
print "$",compute(weekly_income, percent)
print
print "If you stick to this you will go places!!! :)"
 
H

Heather Coppersmith

I just started using python a few weeks ago. This is my first
program. You input your income and what percent you want to save
then it tells you how much to put away. The the code works, but
it puts in the word "none" after it calls the function. Is the
call sytax incorrect? I'd appreciate any feedback you could
offer .

[ ... ]
weekly_income = income/52
def compute(income, percent):
result = income*percent/100
print result

Note that this function prints its result and then returns None.
print "Ok,",first_name + " " + last_name
print
print "This is how much you should put away a week:"
print "$",compute(weekly_income, percent)

OTOH, it looks like this print statement expects the function to
return its result.
print
print "If you stick to this you will go places!!! :)"

The simplest (and most general) solution would seem to be to
define the function to return its result:

define compute(income, percent):
result = income * percent / 100
return result

Then the print statement will work as written.

Regards,
Heather
 
G

Gregor Lingl

Additionally please notice, that your program will produce
incorrect and probably at first irritating results:

**** example output: ****

How much do you make a year after taxes? 5199

What percent of your income do you want to put aside? 1

Ok, Gregor Lingl

This is how much you should put away a week:
$ 0

****

This comes from using integer-constants together with
inputting integers, which causes the /-operator to
perform integer division.

A simple way to avoid this would be to use 52.0 and
100.0 instead of 52 and 100

Regards,
Gregor
 
D

David Eppstein

Additionally please notice, that your program will produce
incorrect and probably at first irritating results: ....
This comes from using integer-constants together with
inputting integers, which causes the /-operator to
perform integer division.[/QUOTE]

Arguably, any code that uses / with integer arguments is now buggy,
because it will produce different results in different Python versions.
You should either use // (integer division) or force floating point
division (e.g. by changing the expression to income*percent/100.0).
 
G

Gerrit Holl

David said:
Arguably, any code that uses / with integer arguments is now buggy,
because it will produce different results in different Python versions.
You should either use // (integer division) or force floating point
division (e.g. by changing the expression to income*percent/100.0).

Or, of course, use from __future__ import division. That is always
the first line of any of my files, I can't live without it :)

Gerrit.
 
T

Tim Rowe

Arguably, any code that uses / with integer arguments is now buggy,
because it will produce different results in different Python versions.
You should either use // (integer division) or force floating point
division (e.g. by changing the expression to income*percent/100.0).

What's the behaviour of "from __future__ import division" on versions
that are too old to know about the future of division?
 
D

David Eppstein

Arguably, any code that uses / with integer arguments is now buggy,
because it will produce different results in different Python versions.
You should either use // (integer division) or force floating point
division (e.g. by changing the expression to income*percent/100.0).

What's the behaviour of "from __future__ import division" on versions
that are too old to know about the future of division?[/QUOTE]

A nice error message.
So that's another good way to go.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top