Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.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编程中的循环错误;游戏“;模块_Python_Loops - Fatal编程技术网

Python:绝对初学者Python编程中的循环错误;游戏“;模块

Python:绝对初学者Python编程中的循环错误;游戏“;模块,python,loops,Python,Loops,我正在学习Python,在这段代码中遇到了一个障碍。我甚至删除了作者的代码,它也有同样的问题( 在导入“随机”模块和“游戏”模块(其中包括“ask_number()”函数和“Player”的类定义)后,我们有: “ask_number()”函数如下所示: def ask_number(question, low, high): """Ask for a number within a range.""" response = None while response not

我正在学习Python,在这段代码中遇到了一个障碍。我甚至删除了作者的代码,它也有同样的问题(

在导入“随机”模块和“游戏”模块(其中包括“ask_number()”函数和“Player”的类定义)后,我们有:

“ask_number()”函数如下所示:

def ask_number(question, low, high):
    """Ask for a number within a range."""
    response = None
    while response not in range(low, high):
        response = int(input(question))
    return response
然而,当程序运行时,“有多少玩家?(2-5):”这个问题会无限期地出现,不管作为响应输入的是什么数字。显然,似乎设置了某种错误的循环,但我无法理解这对我的一生是什么(这就是为什么我是一个“绝对初学者”,哈哈!)

提前感谢你让我恢复理智!:)

编辑: 因为我认为问题仅仅在于ask_number()函数的语法,所以我不想附加很多无关的代码。吸取了教训!:)这是一个完整的循环,所以它似乎又有一个可变的值。(请注意,“ask_yes_no()”函数也在“games”模块中。)

你是说

while again != "n":

但是你再也没有定下来!因为再也不等于“n”,所以它永远不会退出循环

您再也不会更改
的值。为了解决这个问题,你可以添加这个

again = raw_input("Play again?: ")

while
循环结束时。

如果您使用的是Python 2.7,则应使用
raw\u input
而不是
input
,因为您再也不会将变量设置为“n”。这会导致你的while循环迭代到无穷大,并且“有多少玩家”提示会不断出现。另外请注意,5实际上不在
范围(2,5)
,因为这只是2-4。看起来它应该可以工作。您能否确认您试图输入
2
3
4
作为玩家数量,并且您从未看到“玩家姓名:”提示?已确认,Anym。尽管输入2、3或4作为答案,但它只是在“有多少玩家”问题上不断循环。
while again != "n":
again = raw_input("Play again?: ")