View Single Post
Old Oct 4th, 2005, 6:33 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,868
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Quote:
Originally Posted by snafu
1. In order for the math to come out right I need the tax_rate entered by the user to be a decimal (they enter 25 and is read by the program as .25). How can I accomplish this?
Another word for a decimal type variable, is a float. A number can be converted to float, by calling it in the function float().

Example:
x = 25
x = float(x) / 100.0
print x

That would turn 25 in to 0.25 . Because 25 is converted to 25.0, then divided by 100.0 to get 0.25.

Quote:
Originally Posted by snafu
2. How do you insert a variable mid sentence and then have the sentence continue on? I can't quite figure that out. The last two lines I would like to combine into one but am getting syntax errors.
You need to wrap commas (',') around the variable:

a = 1
b = 2
c = 3
print 'A is', a, '. B is', b, '. C is', c, '.'

You could also concact them as strings:

a = 1
b = 2
c = 3
print 'A is ' + str(a) + ' . B is ' + str(b) + ' . C is ' + str(c) ' .'

str() converts any type to string. Which is the only capable type of being concated.

And I don't want to overload you, but the final and best (IMO) option is:

a = 'One'
b = 2
c = 3.0
print 'A is %s. B is %d. C is %s.'%(a, b, c)

%s represents a call to a string (but works with anything), %d a call to an integer

Those calls are filled in after the literal inside %().

Quote:
Originally Posted by snafu
3. How can I clean up this code? Am I making something too difficult or can something be done more efficiently?
Cleanliness does not apply much for a simple series of executions. But something that would be useful to know in the future. Is to get all your inputs in one section, mathematical calculations in another section, and output all your results in the last.

The way you have it it's a bit scrambled. And your output is a little messy, but see the answer to #2 for answers to that.

#!
##  This program prints
##  your name and also
##  calculates your net
##  and gross annual
##  income.

#get input
first_name = raw_input("What is your first name?")
last_name = raw_input("What is your last name?")
hourly_wage = input("How much do you make per hour?")
tax_rate = input("How much would you say is withheld from your paycheck for taxes (percentage)?")

#analyze input
gross_annual = hourly_wage*2000
taxed = gross_annual * tax_rate
net_annual = gross_annual - taxed

#send output
print "%s %s, your approximate gross annual income is %s."%(first_name, last_name, gross_annual)
print "Also, assuming a federal income tax rate of %s, your annual net income is %s."%(tax_rate, net_annual)

Congrats on your first program.

Hope I helped.




BTW. For future reference. input() can be a bad command to use in your programs. If they enter a character, your program crashes. Here's a quick way to fix that:

try:
    number = input('Enter a number: ')
    print "Your number is", number
except NameError:
    print "I said number fool!!"

except will except the mentioned error type and execute the indented block under except, as long as that error fell under the indented block in try. Understand?

Last edited by Sane; Oct 4th, 2005 at 6:45 PM.
Sane is offline   Reply With Quote