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()

Dani AI

Generated

The OP wanted a loop that ends when the user types a sentinel like "done" and also noticed the program never actually calculates pay. tried a loop but used is for comparison (see 's correct warning: is tests identity, not value). suggested recursion; that works but is less appropriate than an explicit loop for repeated input. Key gaps in the existing code: inputs should be validated and converted to numbers, calchours sets values but never returns or uses them, and there should be a clear place to compute and show pay.

A compact, robust pattern (Python 3) is: read a line, treat "done" (or "exit") as the sentinel, convert to float with try/except, enforce sensible bounds (0–60), compute regular vs overtime hours, and return/print the computed pay. The example below uses a configurable overtime multiplier and raises a clear error if hours exceed the allowed maximum.

def get_number(prompt):
    while True:
        s = input(prompt).strip().lower()
        if s in ("done", "exit"):
            return None
        try:
            v = float(s)
            if v < 0:
                print("Enter a non-negative number.")
                continue
            return v
        except ValueError:
            print("Please enter a number or 'done'.")

def calc_pay(hours, rate, overtime_rate=1.5, max_hours=60):
    if hours > max_hours:
        raise ValueError("Can't exceed {} hours.".format(max_hours))
    regular = min(hours, 40)
    overtime = max(0, hours - 40)
    return regular * rate + overtime * rate * overtime_rate

Use those helpers inside a while True loop and break when get_number returns None. Notes: in Python 2 replace input() with raw_input() and use print statements; do not rely on is for string comparison; prefer returning values from helper functions instead of mutating local variables without returning them. Add tests for negative input, malformed input, and the >60-hours case.

Recommended Answers

All 5 Replies

Member Avatar for Member #361407

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 Member #361407

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.