[英]Why did I get an infinite loop (python)?
I'm new to programming.我是编程新手。 I'm trying to write a program with 3 lists.
我正在尝试编写一个包含 3 个列表的程序。 There are no while loops in the code (see below).
代码中没有 while 循环(见下文)。
I tried saving the user's answers in a list called 'user_answers'.我尝试将用户的答案保存在名为“user_answers”的列表中。 The for loop is supposed to loop only 5 times (which is the length of the list), but it seems like I got an infinite loop instead.
for 循环应该只循环 5 次(这是列表的长度),但似乎我得到了一个无限循环。 Any insight on how to fix this?
关于如何解决这个问题的任何见解?
def user_answer():
user_answers=[0, 0, 0, 0, 0]
print('\n')
for i in range (len(user_answers)):
print('\nPlease enter the answer to question', i+1, ':', sep=' ', end='')
user_answers[i]=input()
return user_answer()
Because you return that function name user_answer()
that makes it running recursive indefinitely.因为您返回了 function 名称
user_answer()
,这使得它无限期地递归运行。
Therefore, you should do return user_answers
instead of return user_answer()
.因此,您应该
return user_answers
而不是return user_answer()
。
If you want to make the array having exact size=5, you can loop it 5 times instead of mutating the array every time.如果要使数组的大小为 5,则可以循环 5 次,而不是每次都改变数组。 You can put the instruction for user input in the
input()
as well.您也可以将用户输入指令放在
input()
中。 This will make your code more simple.这将使您的代码更简单。 You can try this example:
你可以试试这个例子:
def user_answer():
user_answers=[]
for i in range(5):
user_answers.append(input("Please enter the answer to question "+str(i+1)+": "))
print()
return user_answers
print(user_answer())
PS If you want to have 3 lists that each contains 5 elements, you can call the function user_answer()
3 times or putting it in a loop. PS 如果你想要 3 个列表,每个列表包含 5 个元素,你可以调用 function
user_answer()
3 次或将其放入循环中。
There is no need to create the list before you begin:在开始之前无需创建列表:
def user_answer():
user_answers=[]
print('\n')
for i in range(5):
print('\nPlease enter the answer to question', i+1, ':', sep=' ', end='')
user_answers.append(input())
return user_answers
Your code return by calling the function, resulting in infinite loop.您的代码通过调用 function 返回,导致无限循环。 I guess what you mean is to return the variable user_answers not to return the function user_answers()
我猜你的意思是返回变量 user_answers 而不是返回 function user_answers()
return user_answers
This might happen if you confuse function and variables with the same name.如果您混淆 function 和具有相同名称的变量,则可能会发生这种情况。 You might want to check function-and-variable-with-the-same-name
您可能需要检查具有相同名称的函数和变量
This happens because you're returning the function itself which means you're creating a infinite recursive function.发生这种情况是因为您要返回 function 本身,这意味着您正在创建无限递归 function。
def user_answer():
user_answers=[0, 0, 0, 0, 0]
print('\n')
for i in range (len(user_answers)):
print('\nPlease enter the answer to question', i+1, ':', sep=' ', end='')
user_answers[i]=input()
return user_answers
print(user_answer())
Try changing return user_answer()
line to return user_answers
尝试更改
return user_answer()
行以return user_answers
I hope you're trying to return the user answers array for further work.我希望您尝试返回用户答案数组以进行进一步的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.