Cod*_*ser 0 python if-statement
我正在尝试使用python构建一个程序,每当我使用这些elif语句时,它们都无法工作.就像我投入500,freq然后我得到Sorry输出,bad而不是我应该得到.
我究竟做错了什么?
freq = raw_input()
if freq <= "87.10" :
print("Sorry")
elif freq <= "108.10" :
print("bad")
else:
print("good")
Run Code Online (Sandbox Code Playgroud)
你正在比较叮咬; 这些是按字典顺序进行比较,而不是数字.'5'以前排序'8',不管后面是什么,就像Alpha之前排序一样Beta,因为A之前B,无论其他字符如何.
比较数字:
freq = float(raw_input())
if freq <= 87.10:
print "Sorry"
elif freq <= 108.10:
print "bad"
else:
print "good"
Run Code Online (Sandbox Code Playgroud)
注意:您使用的是Python 2(或者raw_input()不可用),因此print是一个语句,而不是一个函数.