Good evening,
I was wondering how I could set up a loop for the following code that will exit the loop when the person types done. Would the while command work to do that?

def hours():
    hours = input("How many hours did they work?: ")
    return hours
 
def payrate():
    payrate = input("How much is their payrate?: ")
    return payrate
 
def calchours(pr, h):
    if h > 40:
        rh = 40
        oh = h - 40
        if h > 60:
            print "Can't exceed 60 hours in a week!"
    else:
        rh = h
        oh = 0
    
def main():
    hrs = hours()
    pyr = payrate()
    calchours (pyr, hrs)
 
main()

Recommended Answers

All 5 Replies

Member Avatar for leegeorg07

this is my code:

def hours():
    hours = input("How many hours did they work?: ")
    return hours
 
def payrate():
    payrate = input("How much is their payrate?: ")
    return payrate
 
def calchours(pr, h):
    if h > 40:
        rh = 40
        oh = h - 40
        if h > 60:
            print "Can't exceed 60 hours in a week!"
    else:
        rh = h
        oh = 0
    
def main():
    a ="True"
    while a is "True":
        hrs = hours()
        pyr = payrate()
        calchours (pyr, hrs)
        a = raw_input("is that all( False for yes, True for no)")
 
main()

ive tested it a bit, but ive noticed theres nothing that calculates the pay

Hi, you can use this code instead of going for while loop.

def hours():
hours = input("How many hours did they work?: ")
return hours

def payrate():
payrate = input("How much is their payrate?: ")
return payrate

def calchours(pr, h):
if h > 40:
rh = 40
oh = h - 40
if h > 60:
print "Can't exceed 60 hours in a week!"
else:
rh = h
oh = 0

def main():
hrs = hours()
pyr = payrate()
calchours (pyr, hrs)
a = raw_input("is that all( False for yes, True for no)")
if a == 'yes':
main()
elif a =='no':
quit
main()

Member Avatar for leegeorg07

please use the code tags

Thank you for your responses. I really appreciate them.

This "is" operator is meant to be used to test for the same object, not equality. Try this on your computer
a=257
print a is 257
<prints "False">
So it can give you unexpected results. You want to use:

a = True
    while a :
        a = False   ## (to exit)
##
##--- or
    a ="True"
    while a == "True":
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.