简体   繁体   中英

Attempting to set variable based on conditional statement in Python

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.

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 if s 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...)")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM