答案 0 :(得分:3)
您正在将字符串与int进行比较。使用int casting:
hour=raw_input("Enter Hours")
rate=raw_input("Enter rate")
if int(hour) <= 40:
# for less than 40 this part not executing
pay = float(hour)*float(rate)
print pay
else:
pay = (40*int(rate))+((float (hour)-40)*15)
print pay
答案 1 :(得分:1)
raw_input()
返回字符串,因此if hour <= 40:
会执行if '10' <= 40
之类的操作(假设输入为10
):
>>> foo = raw_input()
12
>>> type(foo)
<type 'str'>
>>>
如果您使用的是Python 3,它会引发TypeError
:
>>> foo = '10'
>>> foo <= 40
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: unorderable types: str() <= int()
>>>
但是在Python 2.x上,输出总是False
:
>>> foo = '10'
>>> foo <= 40
False
>>>
因为输出始终为False
,所以else
部分也始终运行。
您可以将其翻译为int
或float
,如下所示:
hour = float(raw_input("Enter Hours: "))
rate = float(raw_input("Enter rate: "))
if hour <= 40 :
# for less than 40 this part not executing
pay = hour*rate
print pay
else:
pay = (40*int(rate))+((hour-40)*15)
print pay