Python 2.7.2 if /或意外行为

Kie*_*ran 1 python logic

我正在学习Python,我遇到了一个问题.观察此代码:

while 1:
    print "How many lines do you want to add to this file?"

    number_of_lines = raw_input(">").strip()

    if not(number_of_lines.isdigit()) or number_of_lines > 10:
        print "Please try a number between 1 and 10 inclusive."
        continue
Run Code Online (Sandbox Code Playgroud)

代码向用户询问数字,并检查其有效性.但是由于某些原因,即使用户输入的有效数字小于10,代码也始终显示错误.

我可能在某个地方犯了一个小错误,但我无法弄明白......作为一个python新手!

希望你能帮忙!提前致谢.

Gre*_*ill 5

返回时raw_input,您的number_of_lines变量是一个字符串.在与10比较之前,您需要将其转换为整数:

not(number_of_lines.isdigit()) or int(number_of_lines) > 10
Run Code Online (Sandbox Code Playgroud)