user3578390 user3578390 - 29 days ago 10
Python Question

Mismatch but don't know what is wrong...(python)

So I need help with programming.

My assignment is this:

Write a program to prompt the user for hours and rate per hour using raw_input to compute gross pay. Award time-and-a-half for the hourly rate for all hours worked above 40 hours. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use raw_input to read a string and float() to convert the string to a number. Do not worry about error checking the user input - assume the user types numbers properly.

I did this:

inp = raw_input ('Enter Hours: ')
hours = float(inp)
inp = raw_input ('Enter Rate: ')
rate = float(inp)

print rate, hours

if hours <= 40 :
pay = hours * rate
else :
pay = rate * 40 + (rate * 1.5 * ( hours - 40 ))

print pay


And it seemed to be okay but when I click on check the code, I enter hours 45, and then rate I tried entering 10.50, 10.5 but every time I get this:
10.5 45.0 ← Mismatch
498.75

The answer 498.75 is correct but I keep getting mismatch there so I cannot finish my assignment. Anyone knows what am i doing wrong?

Answer

To print float with your format you should use format string (examples).

So you should change line:

print rate, hours

to:

print("Rate = %.2f, Hours = %.0f" % (rate, hours))
#               ^             ^
#               |             Remove all chars after point (may be you need to change that 
#                                                           according your task) 
#               Use to chars after comma (no zeros removing)
Comments