为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

烙印99 2024-01-27 14:51:47
我对编码真的很陌生(大约 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 循环有关,但正如我所说,我对此很陌生,这一切都让人感到不知所措,试图用谷歌搜索这似乎会带来我不明白的超级复杂的事情! !谢谢
查看完整描述

4 回答

?
婷婷同学_

TA贡献1844条经验 获得超8个赞

第一:为什么你得到 [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():

    mylist = []

    while len(mylist) < 10:

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

        

        if (0 < n < 100):

            mylist.append(n)

        else:

            print('This is not between 0 and 100 !')


    return mylist

    

    

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

然后系统会询问您从 0 到 100(不含)的数字,直到您得到 10 的尺寸。


查看完整回答
反对 回复 2024-01-27
?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

好的,您正在尝试使用while而不是if. 这就是我要做的 - 我很快就会解释:


def get_user():

    mylist = []

    for i in range(5):

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

        if n <= 100 and 0 <= n:

            mylist.append(n)

            

        else:

            print("This is not between 0 and 100 !")

            i = i - 1


    return mylist

浏览代码

for i in range(5)


这里我们使用for来迭代range 数字 (0 - 5)。for将分配i给范围内的最小数字,并在i每次迭代中不断加 1,直到 i超出范围range。请参阅此了解更多信息range


if n <= 100 and 0 <= n:


这里and检查2 个表达式是否为真。在这种情况下,如果 n 小于或等于百且大于或等于 0。您可以使用任意and次数,如下所示:


if 1 < 100 and 100 > 200 and 20 < 40:

   print("foo!")

现在看看else:


else:

    i = i - 1

在这里,我们减去 1,i从而将我们的进度减少 1。另请注意,我的函数被调用get_user()而不是getUser(). 在Python中,我们使用蛇形命名法而不是驼峰命名法。


我希望这个答案有帮助。我在回答中给出了一些小提示。你应该用谷歌搜索他们!我也不会直接给你逻辑。试着弄清楚吧!这样你就能很好地理解我所做的事情。


查看完整回答
反对 回复 2024-01-27
?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

def getUser(n):

mylist = []

t = True

while t:

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

    if n >0 and n<=100:

        mylist.append(n)

    if len(mylist) == 10:

        return(mylist)

        t = False

    elif n < 0  or n >100:

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

    

        



查看完整回答
反对 回复 2024-01-27
?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

所以这就是你做错了什么


def getUser(n):

    mylist = []

    while 0 < n < 100:

        mylist.append(n)

        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))

您注意到编辑后的代码有什么不同吗?


您从未重新分配变量的值n ,因此您只需添加getUser每次传递给的任何值(并且每次都是相同的值)


如果您遇到困难,不要害怕寻求帮助!


PS 如果您能够应对挑战,您也可以将一条线从某个地方移动到其他地方,以使其表现得更好一点;)


查看完整回答
反对 回复 2024-01-27
  • 4 回答
  • 0 关注
  • 40 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信