0

So today is literally my first day of learning a programming language so I'm very new to Python. I'm taking the online Python for Informatics course at the University of Michigan and our first real assignment is to create a simple gross pay rate calculator.

That was simple enough so I decided I wanted to expand what the program could do to calculate net pay and account for taxes. The trouble I'm having is determining how I can dynamically (if that's even the right word) set the value of the variable "taxrate" based on a series of conditional statements.

I have yet to find the answer in searching through Python's site and Stack Overflow. I think my limited understanding of programming probably is limiting my ability to interpret properly what I have found.

Just looking for a little help:

Code:

#This program is intended to calculate the net pay of employees
#This first section includes a loop for mistakes and finds gross pay

while True:

hours = raw_input('How many hours do you work weekly?')
hours1 = float(hours)
rate = raw_input('What is your hourly rate of pay?')
rate1 = float(rate)
grosspay = hours1 * rate1
taxstatus = raw_input('Do you pay taxes?')


#This secdtion is establishing the tax bracket the user falls into
taxbracket = taxrate
if grosspay <= 1000:
    taxrate = 0.90
if grosspay > range(1000,1500):
    taxrate = 0.78
if grosspay >= 1501:
    taxrate = 0.63

# This section is intended to calculate pay after taxes
grosspay = hours1 * rate1
if taxstatus == 'yes':
    netpay = grosspay * taxrate
print'Your weekly pay after taxes is',netpay
if not taxstatus:
    print grosspay

When I run this in PyCharm it lets me know that "taxrate" hasn't been defined. I ultimately want the program to set the "taxrate" based on what the users "grosspay" is. Is what I'm trying to do possible? I'm assuming it is and that I just don't understand how to do it.

Any help is greatly appreciated and in case someone is wondering what the loop is for I've got a user error checker that I'm working on after this portion of the program completes.

4
  • Indentation is very important in python Commented Jul 19, 2015 at 4:29
  • taxbracket = taxrate get rid of this line, since you're not using taxbracket anywhere Commented Jul 19, 2015 at 4:29
  • First, the line taxbracket = taxrate is where your problem is, since you haven't yet defined taxrate. But you never use taxbracket either, so you can delete that line entirely. Second, you'll need to change if grosspay > range(1000,1500): to if grosspay <= 1500: Commented Jul 19, 2015 at 4:30
  • Pynchia In PyCharm everything is indented appropriately. I wasn't certain syntactically perfect indentation was necessary for understanding my question. Still a good note for a beginner :) NightShadeQueen Thanks. @DavidRobinson Thanks. Will that conflict with the previous statement of if grosspay <= 1000: taxrate = 0.90 ? Commented Jul 19, 2015 at 4:35

1 Answer 1

1

Your logic is a bit suspect in if grosspay > range(1000, 1500). What would it mean to be "greater" than a range of numbers? my guess is that the grosspay you input is, in fact, within the range [1000, 1500), so it hits this logic bug in your code and fails to assign it to anything.

The usual way to check if a number is within a range is to use the in operator.

if some_num in range(1, 10):
    print("some_num is 1, 2, 3, 4, 5, 6, 7, 8, or 9")

However you'll notice that some_num MUST be contained in the integer range [1, 9] for this to trigger. If some_num is 7.5, this will fail. This is incredibly likely in the case of gross pay. What are the chances of someone's pay coming out to an exactly even dollar amount?

Instead what you could do is:

if grosspay <= 1000:
    taxrate = 0.90
elif 1000 < grosspay <= 1500:
    taxrate = 0.78
elif 1500 < grosspay:
    taxrage = 0.63

using elif instead of a series of ifs makes the code slightly more efficient, since if/elif/else is by definition one block that is mutually exclusive. In other words:

a = 1
b = 2

if a == 1:
    print("This gets done!")
if b == 2:
    print("This gets done!")

if a == 1:
    print("This gets done!")
elif b == 2:
    print("BUT THIS DOESN'T!")
else:
    print("(this doesn't either...)")
Sign up to request clarification or add additional context in comments.

1 Comment

I honestly wasn't certain how to create a series of numbers that I wanted my comparison to consider. Given where I am in the class I hadn't been familiarized with elif but that's very helpful. That worked like a Charm! Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.