如果语句只打印False

时间:2013-06-28 08:20:51

标签: python if-statement

我想让白天打印出真或假。无论“日期”的整数是什么,它目前只打印False。我是Python的新手,所以如果这是一个菜鸟监督,请耐心等待。

def date():
    date = raw_input("Date (ex. Jun 19): ")
    date = date.split(' ')
    month = date[0]
    month = month[:3].title()
    day = date[1]
    day.isdigit()
    if day < 10:
            print "True"
    else:
            print "False"

3 个答案:

答案 0 :(得分:7)

day是一个字符串,在Python 2中是any string compares greater than any number

>>> "0" > 1
True
>>> "" > 100000000000000000000
True

在Python 3中已经改变了这种(一致但任意的)行为:

>>> "" > 100000000000000000000
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() > int()

答案 1 :(得分:5)

raw_input会返回一个字符串,因此您应首先将其转换为int

day = int(date[1])

答案 2 :(得分:5)

python 2中的

raw_input返回一个字符串 然后你将一个字符串与一个int进行比较,这就是为什么你会得到假的

使用int关键字将str转换为int

if int(day) < 10:
像这样

相关问题