Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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 while循环/范围_Python - Fatal编程技术网

Python while循环/范围

Python while循环/范围,python,Python,我需要向用户询问一个数字,如果它在低/高数字的范围内,则返回该数字,如果不在该范围内,则循环直到输入的数字在该范围内。我真的不知道该怎么做,但我想我有一部分是对的。我主要关心的是行“while question!=low在这种情况下,最简单的解决方案是使用True作为while循环中的条件,如果数字正确,则在循环中使用if: def ask_number(low, high): while True: try: number = int(raw_i

我需要向用户询问一个数字,如果它在低/高数字的范围内,则返回该数字,如果不在该范围内,则循环直到输入的数字在该范围内。我真的不知道该怎么做,但我想我有一部分是对的。我主要关心的是行“while question!=low在这种情况下,最简单的解决方案是使用
True
作为
while
循环中的条件,如果数字正确,则在循环中使用
if

def ask_number(low, high):
    while True:
        try:
            number = int(raw_input("Enter a number within the range: "))
        except ValueError:
            continue
        if low <= number <= high:
            return number
def ask_编号(低、高):
尽管如此:
尝试:
number=int(原始输入(“输入范围内的数字:”)
除值错误外:
持续

在这种情况下,如果低,最简单的解决方案是使用
True
作为
while
循环中的条件,如果数字很好,则在循环中使用
if

def ask_number(low, high):
    while True:
        try:
            number = int(raw_input("Enter a number within the range: "))
        except ValueError:
            continue
        if low <= number <= high:
            return number
def ask_编号(低、高):
尽管如此:
尝试:
number=int(原始输入(“输入范围内的数字:”)
除值错误外:
持续

如果low您这样想,while循环语法会更清晰:“当用户的答案小于low或大于high时,我想继续询问用户的答案。”直接翻译成Python,这将是

while question < low or question > high:
您的最终代码应该如下所示:

def ask_number(low, high):
    assert low < high
    question = int(raw_input("Enter a number within the range: "))
    while question < low or question > high:
        question = int(raw_input("Enter a number within the range: "))
    return question

print(ask_number(5,20))
def ask_编号(低、高):
断言低<高
问题=int(原始输入(“输入范围内的数字:”)
当问题<低或问题>高时:
问题=int(原始输入(“输入范围内的数字:”)
返回问题
打印(询问编号(5,20))

如果您这样想,您的while循环语法将更加清晰:“我希望在用户的答案小于低或大于高时,继续询问用户的答案。”直接翻译为Python,这将是

while question < low or question > high:
您的最终代码应该如下所示:

def ask_number(low, high):
    assert low < high
    question = int(raw_input("Enter a number within the range: "))
    while question < low or question > high:
        question = int(raw_input("Enter a number within the range: "))
    return question

print(ask_number(5,20))
def ask_编号(低、高):
断言低<高
问题=int(原始输入(“输入范围内的数字:”)
当问题<低或问题>高时:
问题=int(原始输入(“输入范围内的数字:”)
返回问题
打印(询问编号(5,20))
def ask_编号(低、高):
“”“问题不能小于最小值,因此我们将其设置在
使循环至少执行一次的最小值。”“”
问题=低-1
“”“您希望问题位于范围[高,低](注意
包容性),这在数学上类似于“低<代码>定义询问数量(低,高):
“”“问题不能小于最小值,因此我们将其设置在
使循环至少执行一次的最小值。”“”
问题=低-1
“”“您希望问题位于范围[高,低](注意

(包容性),这在数学上看起来像“低,我正在尝试创建这个游戏,但我得到了我的答案谢谢我正在尝试创建这个游戏,但我得到了我的答案谢谢这是个坏主意
object并不是
操作符中的
的特例,因此它将在整个范围内循环。请尝试在xrange(200000000)
中使用
100000000来了解我的意思。:)还要注意,这对Python3.x
range()
对象有效。这是一个坏主意。Python2.x
xrange()
object对
操作符中的
没有特殊情况,因此这将在范围内循环。请尝试在xrange(200000000)
中使用
100000000来了解我的意思。:)还要注意,这对Python 3.x
range()
对象有效。如果他只想接受整数(甚至不想接受(通过截断)然后他可以做一些类似于
if number.isnumeric():return int(number)
@TylerCrompton:你可能是指
number.isdigit()
,因为
isnumeric()
不存在。这甚至可以排除前导或尾随空格,所以可能有点太严格了。如果他只接受整数(甚至不接受(通过截断)浮动,那么他可以做一些类似于
if number.isnumeric():return int(number)
@TylerCrompton:你可能是指
number.isdigit()
,因为
isnumeric()
不存在。这甚至会排除前导或尾随空格,因此可能有点太严格。仅供参考,此代码使用三个参数调用
ask\u number()
,但只接受两个。@AustinMarshall感谢您捕捉到这一点。仅供参考,此代码调用
ask\u number()
有三个参数,但它只接受两个。@Austinmars感谢您捕捉到这一点。他对
问题=“
的使用(在Python2上)效果很好,因为它永远不会在
低的范围内,但您无法将否定连成一条(除非您想做
而不是()
。是的,我理解使用
”“
很好。他对
question=”“
的使用效果很好(在Python 2上),因为它永远不会在
低的范围内,但你不能将否定连成一条链(除非你想做
而不是()
。是的,我知道使用
很好。
def ask_number(low, high):

    """question cannot be less than the minimum value so we set it below the
    minimum value so that the loop will execute at least once."""

    question = low - 1

    """You want question to be within the range [high, low] (note the
    inclusivity), which would mathematically look like 'low <= question <= high'.
    Break that up into what appears to be one comparison at a time:
    'low <= question and question <= high'. We want the while loop to loop when
    this is false. While loops loop if the given condition is true. So we need to
    negate that expression. Using the DeMorgan's Theorem, we now have
    'low < question or question > high'."""

    while question < low or question > high:

        """And this will obviously update the value for question. I spruced up
        the only argument to raw_input() to make things a bit 'smoother'."""

        question = int(raw_input("Enter a number within the range [%d, %d]: " % _
                   (low, high)))

    # Return question.
    return question