Question

Rewrite the Python code and: -Call all the functions in " main" function. -Use try/except (or...

Rewrite the Python code and:

-Call all the functions in " main" function.

-Use try/except (or other checking inputs designs) inside the get_input function to check the user inputs.

-Check your code for any invalid inputs: string inputs and also negative numbers

def get_input():
    n = int(input("Enter Hours: "))
    rate = float(input("Enter Rate: "))
    return n, rate


def compute_pay(hours, rate):
    if hours <= 40:
        return hours * rate
    else:
        return (40 * rate) + ((hours - 40) * rate * 1.5)


def print_output(payment):
    print("Pay: " + str(payment))

def main():
    the_hours, the_rate = get_input()
    the_pay = compute_pay(the_hours, the_rate)
    print_output(the_pay)


main()
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment....

def get_input():

    while True:

        try:

            n = int(input("Enter Hours: "))

            if n<0:

                print("Hours can't be negative! Reenter: ")

            else:

                break

        except ValueError:

            print("Hours must be integer")

    while True:

        try:

            rate = float(input("Enter Rate: "))

            if(rate<0):

                print("Rate must be positive")

            else:

                break

        except ValueError:

            print("rate must be integer!")

    return n, rate


def compute_pay(hours, rate):

    if hours <= 40:

        return hours * rate

    else:

        return (40 * rate) + ((hours - 40) * rate * 1.5)


def print_output(payment):

    print("Pay: " + str(payment))

def main():

    the_hours, the_rate = get_input()

    the_pay = compute_pay(the_hours, the_rate)

    print_output(the_pay)


main()

Add a comment
Know the answer?
Add Answer to:
Rewrite the Python code and: -Call all the functions in " main" function. -Use try/except (or...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT