View Single Post
Old Oct 5th, 2005, 5:03 PM   #4
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. Changing the type to floating did the trick but the number is always followed by six zeros (i.e. "Also, assuming a federal income tax rate of 25.000000, ...). While technically correct, how can I make this number less... exact. I would prefer 25.0.
x = 3.141592
x = round(x, 1) #1 = round to 1 digit place
print x
Outputs: 3.1

Quote:
Originally Posted by snafu
2. You said that input() can be a bad command to use. What would you use in place of input()? Raw_input()?
You would use the code example I gave you with try and except. That is if you need integer input. Always use raw_input for string input.

Quote:
Originally Posted by snafu
3. Also, in my program can you specify where the "except NameError:" line should go? Do I need to place it after every line where they could enter a char instead of an int? I tried placing it at the very end but received a syntax error.
Well for something like this, you shouldn't worry too much about safety, but here is what it would look like:

def safe_input(x):
    from sys import exit
    try:
        return input(x)
    except NameError:
        print "I said number fool!!"
        exit()

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

#analyze input
tax_rate = round( float(tax_rate) / 100.0, 1)
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)

I'm not sure if you've used functions yet. But that's a little example of how a function can improve the functionality of your code by breaking repeated steps.

In this case the function is defined as "safe_input". And inside the () we call variable x, or the message that is passed through safe_input(). We return the value from the input call, but if the user entered a string. We use the function exit() from the import sys.

The biggest advantage to using a function, is instead of using exit(), we can just go safe_input(x). That is something called a recursive function. The advantage of this, is it'll keep asking for the same input until it finally gets a number, or else it just recurses upon itself.

def safe_input(x):
    try:
        return input(x)
    except NameError:
        print "Invalid Input"
        safe_input(x)
Neat eh?

It's a little complicated to go much more in to detail. Your tutorial will tell you all about functions, don't be intimidated.

Last edited by Sane; Oct 5th, 2005 at 5:25 PM.
Sane is offline   Reply With Quote