user3578390
user3578390

Reputation: 3

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?

Upvotes: 0

Views: 12525

Answers (6)

Rakesh Gombi
Rakesh Gombi

Reputation: 351

4.6 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of pay in a function called computepay() and use the function to do the computation. The function should return a value. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking the user input unless you want to - you can assume the user types numbers properly. Do not name your variable sum or use the sum() function.

def computepay(h,r):
    if h <= 40:
        return h * r
    elif h > 40:
        return (40 * r + ((h - 40) * 1.5 * r))

hrs = float(input("Enter Hours:"))
rate = float(input("Enter Rate:"))
p = computepay(hrs, rate)
print("Pay",p)

Upvotes: 0

sgr0691
sgr0691

Reputation: 29

hrs = raw_input("Enter Hours:")
h = float(hrs)
rate = raw_input("Enter rate:")
r = float(rate)
pay  = h * r     
print pay

This would be the answer to your question @user3578390

Upvotes: 0

Corneyc
Corneyc

Reputation: 1

I did this:

hrs = raw_input("Enter Hours:")
h = float(hrs)
rate = raw_input("Enter rate:")
r = float(rate)
pay  = h * r 

if h <=40:
    pay  = h * r  
else:
    pay = r * 40 + (r * 1.5 * ( h - 40 ))
    print pay

Upvotes: -1

Subhro Bera
Subhro Bera

Reputation: 11

By using a function you can do it

def computepay(h,r):
    if (h>40) : 
        pay = (40*r)+(h-40)*1.5*r
    else:
        pay = (h*r)     
    return pay
try:
    inp = raw_input("Please enter hours: ")
    hours=float(inp)
    inp = raw_input("Please enter rate: ")
    rate= float(inp)
except:
    print "Please enter a number as input"
    quit()

print computepay(hours,rate)

Upvotes: 1

tripleee
tripleee

Reputation: 189910

It seems that print rate, hours produces output which the checking program does not expect, and cannot cope with. Simply comment out that line.

Upvotes: 0

mblw
mblw

Reputation: 1798

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)

Upvotes: 1

Related Questions