2
def Calc_Salary(x,y):
 Pay = x*y
 return Pay
def Calc_with_bonus(c,d):
 bonus = c+d
 return bonus
hours = raw_input('Number of hours')
rate = raw_input('Rate per hour')
if hours >= 40:
 extras = ((float(hours)-40)*float(rate)*1.5)
 regular = float(rate)*40
 print Calc_with_bonus(extras,regular)
elif hours < 40:
 print Calc_Salary(hours,rate)
raw_input()

Please help, I have gone over the program many times and everything seems to be right. When hours are more than 40 it will calculate bonus for hours over 40 and regular rate for hours up to 40, add them and give right result. However, if hours less than 40, all it has to do is call Calc_Salary and multiply hours*rate which it does, but for example if you enter hours=10*rate=10 instead of 100 it computes -50 please help :(

asked Sep 18, 2015 at 3:50
4
  • What does the last raw_input() do? Commented Sep 18, 2015 at 3:53
  • raw_input() reads your input as strings so '10' >= 40 returns True and are you hitting the first block of the if statement Commented Sep 18, 2015 at 3:56
  • When you double click the program, enter the hours and the rate, it does its job real quick and closes when finished, which prevents user from seeing the result, since it executes too fast, so raw_input() forces python stay open until you click "Enter" Commented Sep 18, 2015 at 3:57
  • danielcorin is there a way to fix that ?? would it work if I define like this?: def Calc_Salary(x,y): Pay = float(x)*float(y) return Pay Commented Sep 18, 2015 at 4:00

1 Answer 1

2

The issue is that hours and rate are string when you compare them against integer 40 , they would always be greater, so even if you enter hours as 10 , you end up inside the if block, and it is trying to calculate the salary as if the hours were greater than 40. Example of strings always being greater than int -

>>> '10' > 123123123
True

Instead of converting the numbers to float after the comparison, you should convert them to float directly as you take input. Example -

hours = float(raw_input('Number of hours'))
rate = float(raw_input('Rate per hour'))

After this you do not need to convert hours/rate to float in the if block.

answered Sep 18, 2015 at 3:55
Sign up to request clarification or add additional context in comments.

3 Comments

Anand S Kumar thanks a lot man, I have invested hours trying to fix that, you answer made perfect sense and I was able to fix my program, as you can tell I am a beginner, so thanks a lot for your help and prompt response. Thanks as well to @danielcorin for your input it helped look in the right direction
Done buddy :) can u let me know how to close the question, since it has been answered??
there is no closing as such , marking the answer should be good enough.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.