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 :(
1 Answer 1
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.
raw_input()do?raw_input()reads your input as strings so'10' >= 40returnsTrueand are you hitting the first block of the if statement