Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 范围()中的数字代码未返回预期结果_Python_Range - Fatal编程技术网

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

Python 范围()中的数字代码未返回预期结果,python,range,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 ("> ")

我正在学习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并没有被定义为一个错误

感谢您的指导。

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

number = int(raw_input ("> "))
原始输入()

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


来自

欢迎使用Python!我猜从输入读取的数字是一个字符串,尝试
int(number)
也是一个很好的机会,可以通过try和except;-)学习健壮的输入处理“这是一个介于1和10之间的数字”不包括10。很好,我已经了解了这一点,但我没有想到。我还意识到,范围当然会忽略范围中的最后一个数字,因此选择“10”也会跳到else语句。我们现在就开始工作。再次感谢