简体   繁体   中英

Enter hours worked Python program

The problem:

Write a program which asks the user to enter an integer 'n' which would be the total numbers of hours the user worked in a week and calculates and prints the total amount of money the user made during that week. If the user enters any number less than 0 or greater than 168 (n < 0 or n > 168) then your program should print INVALID

Assume that hourly rate for the first 40 hours is $8 per hour.

Hourly rate for extra hours between 41 and 50 (41 <= n <= 50 ) is $9 per hour.

Hourly rate for extra hours greater than 50 is $10 per hour.

Here are a few examples:

if the user enters -5, then your program should print INVALID

if the user enters 34, then your program should print YOU MADE 272 DOLLARS THIS WEEK

if the user enters 45, then your program should print YOU MADE 365 DOLLARS THIS WEEK

if the user enters 67, then your program should print YOU MADE 580 DOLLARS THIS WEEK

Note that the amount of money made by the user must be an integer (not a float) and your output should exactly match the format specified above (including spaces and capitalization).

My code:

hours = int(input("how many hours did you work this week? "))

if hours  < 0 or > 168:

    print ("INVALID")

elif hours  <= 40

    Rate = $8

elif hours (<= 41 or <= 50)

    Rate = $9

elif hours >= 50

    Rate = $10

I know it's not done yet but I don't know what to do as next and I can't fix the syntax problem.

if hours  < 0 or > 168:

is incorrect referring to Why does `a == b or c or d` always evaluate to True?

This is a right way to write this statement:

if hours < 0 or hours > 168:

Furthermore, the following is also incorrect:

elif hours (<=41 or <=50)

This statement will only be reached if hours <= 40 thus you can omit checking if hours <= 41 in the incorrect statement.

The following will work right:

elif hours <= 50:

(don't forget : even with elif statements)

CODE:

import sys

hours = int(input("how many hours did you work this week? "))

if hours  < 0 or hours > 168:
    print("invalid")
    #here i used sys.exit so that it can exit after printing invalid
    sys.exit()
elif hours  <= 40 and hours > 0:
    a = hours
    b = 0
    c = 0 
elif hours>40 and hours <= 50:
    a = 40
    b = (hours - a)
    c = 0
elif hours > 50:
    a = 40
    b = 10
    c = hours - (a+b)

rate = (a*8)+(b*9)+(c*10)
print("\n\nYOU MADE {} DOLLARS THIS WEEK".format(rate))

OUTPUT 1:

how many hours did you work this week? 34

YOU MADE 272 DOLLARS THIS WEEK

OUTPUT 2:

how many hours did you work this week? 67

YOU MADE 580 DOLLARS THIS WEEK

I myself has just started learning Python and spent hours looking for a solution of this quiz

@Alpha Green's method help but in the end answer was wrong.

This is what i did which was accepted

    user_entry=int(input("Enter total numbers of hours worked this week "))
    r = user_entry
    s = 0
    t = 0
    if user_entry <0 or user_entry >168 :
        print("INVALID")
    if user_entry <=40 and user_entry >=0 :
        r
        print("YOU MADE",r * 8,"DOLLARS THIS WEEK")
    if user_entry > 40 and user_entry <= 50 :
        r = 40
        s = (user_entry - r)
        print("YOU MADE",(r*8)+(s*9),"DOLLARS THIS WEEK")
    if user_entry >50 and user_entry <=168:
        r = 40
        s = 10
        t=user_entry - (r + s)
        amount=(r * 8) + (s * 9) + (t * 10)

        print("YOU MADE",amount,"DOLLARS THIS WEEK")

OUTPUT # 1

    Enter total numbers of hours worked this week 34
    YOU MADE 272 DOLLARS THIS WEEK

OUTPUT # 2

    Enter total numbers of hours worked this week 67
    YOU MADE 580 DOLLARS THIS WEEK

OUTPUT # 3

    Enter total numbers of hours worked this week -2
    INVALID

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