范围()代码中的数字未返回预期结果

时间:2016-06-14 10:46:09

标签: python range

我正在学习Python,目前正在使用简单的代码编写和if-elif-else语句。 我有这个代码块,总是返回else语句,即使我输入1到30之间的数字。

elif door == "3":
        print "This is a winning treasure room, congratulations!"
        print "Pick a number between 1 and 30"

        number = raw_input ("> ")

        if number in range (1,10):
            print "that's a number between 1 and 10"

        elif number in range (11, 20):
            print "that's a number between 11 and 20"

        elif number in range (21, 30):
            print "that's a number between 21 and 30"

        else:
            print "that's not a number we asked for"

我还尝试过:

elif door == "3":
        print "This is a winning treasure room, congratulations!"
        print "Pick a number between 1 and 30"

        number = raw_input ("> ")

        if number == number in range (1,10):
            print "that's a number between 1 and 10"

        elif number == number in range (11, 20):
            print "that's a number between 11 and 20"

        elif number == number in range (21, 30):
            print "that's a number between 21 and 30"

        else:
            print "that's not a number we asked for"

我也尝试过:

 if number == x in range (1, 10):

但当然x未被定义为错误。

任何指导意见。

1 个答案:

答案 0 :(得分:3)

raw_input()将输入转换为字符串。您需要将其转换为整数。

number = int(raw_input ("> "))

<强>的raw_input()

  

然后该函数从输入中读取一行,将其转换为字符串   (剥离尾随换行符),然后返回。

来自doc