函数仅返回列表的第一个输入,并且在输入超出范围时不继续

我对编码真的很陌生(大约 3 周后),我正在编写一个代码,要求用户输入 0 到 100 之间的数字,然后将该数字添加到列表中。一旦列表的长度达到 10 个数字,它就会打印该列表。我正在使用一个函数来实现这一点。


我可以得到它,以便它打印一个列表,但它只打印输入的第一个数字。例如,如果我输入 5,它仍然会要求其他数字,但会将列表打印为 [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]。另外,如果我输入 0-100 范围之外的数字,它确实会要求您输入不同的数字,但随后它会停止并将列表打印为“无”。


下面是我的代码:


def getUser(n):

    mylist = []

    while 0 < n < 100:

        mylist.append(n)

        int(input("Please enter a number between 0 and 100: "))

        if len(mylist) == 10:

            return(mylist)

    else:

        int(input("This number is not in the range of 0-100, please input a different number: "))

    

    

n = int(input("Please enter a number between 0 and 100: "))

print("The numbers you have entered are: ", getUser(n))

我猜它与 while/else 循环有关,但正如我所说,我对此很陌生,这一切都让人感到不知所措,试图用谷歌搜索这似乎会带来我不明白的超级复杂的事情! !谢谢


烙印99
浏览 43回答 4
4回答

婷婷同学_

第一:为什么你得到 [5, 5, 5, 5, 5, 5, 5, 5, 5, 5] :int(input("Please enter a number between 0 and 100: "))你应该先把这个给 n :n=int(input("Please enter a number between 0 and 100: "))然后,它退出循环,因为您进入了else,然后您不再返回while。然后你的函数结束了,没有return,所以none老实说,我不明白为什么while如果你给出的数字超出范围,你的代码就会跳出循环,因为该值没有给n。如果有人可以在评论中解释,那就太好了!我会这样做:def getUser():&nbsp; &nbsp; mylist = []&nbsp; &nbsp; while len(mylist) < 10:&nbsp; &nbsp; &nbsp; &nbsp; n = int(input("Please enter a number between 0 and 100: "))&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (0 < n < 100):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mylist.append(n)&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('This is not between 0 and 100 !')&nbsp; &nbsp; return mylist&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;print("The numbers you have entered are: ", getUser())然后系统会询问您从 0 到 100(不含)的数字,直到您得到 10 的尺寸。

一只甜甜圈

好的,您正在尝试使用while而不是if. 这就是我要做的 - 我很快就会解释:def get_user():&nbsp; &nbsp; mylist = []&nbsp; &nbsp; for i in range(5):&nbsp; &nbsp; &nbsp; &nbsp; n = int(input("Please enter a number between 0 and 100: "))&nbsp; &nbsp; &nbsp; &nbsp; if n <= 100 and 0 <= n:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mylist.append(n)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("This is not between 0 and 100 !")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i = i - 1&nbsp; &nbsp; return mylist浏览代码for i in range(5)这里我们使用for来迭代range 数字 (0 - 5)。for将分配i给范围内的最小数字,并在i每次迭代中不断加 1,直到 i超出范围range。请参阅此了解更多信息rangeif n <= 100 and 0 <= n:这里and检查2 个表达式是否为真。在这种情况下,如果 n 小于或等于百且大于或等于 0。您可以使用任意and次数,如下所示:if 1 < 100 and 100 > 200 and 20 < 40:&nbsp; &nbsp;print("foo!")现在看看else:else:&nbsp; &nbsp; i = i - 1在这里,我们减去 1,i从而将我们的进度减少 1。另请注意,我的函数被调用get_user()而不是getUser(). 在Python中,我们使用蛇形命名法而不是驼峰命名法。我希望这个答案有帮助。我在回答中给出了一些小提示。你应该用谷歌搜索他们!我也不会直接给你逻辑。试着弄清楚吧!这样你就能很好地理解我所做的事情。

江户川乱折腾

def getUser(n):mylist = []t = Truewhile t:&nbsp; &nbsp; n =int(input("Please enter a number between 0 and 100: "))&nbsp; &nbsp; if n >0 and n<=100:&nbsp; &nbsp; &nbsp; &nbsp; mylist.append(n)&nbsp; &nbsp; if len(mylist) == 10:&nbsp; &nbsp; &nbsp; &nbsp; return(mylist)&nbsp; &nbsp; &nbsp; &nbsp; t = False&nbsp; &nbsp; elif n < 0&nbsp; or n >100:&nbsp; &nbsp; &nbsp; &nbsp; print('This number is not in the range of 0-100, please input a different number:')&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

阿波罗的战车

所以这就是你做错了什么def getUser(n):&nbsp; &nbsp; mylist = []&nbsp; &nbsp; while 0 < n < 100:&nbsp; &nbsp; &nbsp; &nbsp; mylist.append(n)&nbsp; &nbsp; &nbsp; &nbsp; n = int(input("Please enter a number between 0 and 100: "))&nbsp; &nbsp; &nbsp; &nbsp; if len(mylist) == 10:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return(mylist)&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; int(input("This number is not in the range of 0-100, please input a different number: "))&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;n = int(input("Please enter a number between 0 and 100: "))print("The numbers you have entered are: ", getUser(n))您注意到编辑后的代码有什么不同吗?您从未重新分配变量的值n ,因此您只需添加getUser每次传递给的任何值(并且每次都是相同的值)如果您遇到困难,不要害怕寻求帮助!PS 如果您能够应对挑战,您也可以将一条线从某个地方移动到其他地方,以使其表现得更好一点;)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python