我怎么能做一个无限的“为”循环能够保存值,而不使用“while”? [英] How could I make an infinite "For" loop able to save values, without using "while"?

查看:156
本文介绍了我怎么能做一个无限的“为”循环能够保存值,而不使用“while”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Python(使用3.6.2),在我最后一节课上,他们要求我做一些需要为创建一个无限的 循环。出于某种原因,老师不希望我们在整个练习中使用

所以,我一直在寻找一种方法来做到这一点。但是,这也是困难的,因为老师不希望我们使用我们在课堂上没有见过的任何命令。所以我不能使用 .append,sys函数,我甚至不能使用 break 。我必须找到一个处理 simple 命令的方法。



我以为我可以这样做;

  x = 1 
在范围内(x):
do_something()
x + = 1

然而,它似乎没有工作。我认为这是因为Python不读取范围的值再次?

我找不到方法,但经过几个小时的思考,我发现自己一个小的解决方法我可以使用:

pre $ def ex():
print(Welcome)
for i in (1):
math = int(输入(请插入在Math中获得的分数,或者插入666退出))
if(math> 0 and math <60):
print(Sorry。You failed the test)
return ex():
elif(math> = 60 and math <= 100):
print(你通过了测试)
return ex():
elif(math == 666):
return exit()
else:
print(ERROR:请输入一个有效的号码)
return ex():

def exit():
pass

正如你所看到的,是什么让它无限是它返回直到你通过输入666告诉程序exit。我还想有一个更合适的方法来退出该函数。

我仍然想知道是否有更好的方法来制作我的循环无限,直到用户调用它停止。然而,这样或那样的方式我得到了这个练习的工作。问题出现在我开始使用第二个时,或多或少如此:


想象一下同样的过去的节目,但这一次,它不会只是告诉你,如果你通过了测试或没有。它希望收集通过输入输入的许多成绩,然后计算所有成绩的平均成绩。我无法保存这些值(等级),因为我每次都会重新启动自己的函数。


根据老师的指示,我不能问用户他要我多少等级计算 即可。它必须是无限的,并不断要求输入,直到用户选择不。

我真的卡住了,失去了它。这是非常困难和令人沮丧的,因为它会更容易,如果我们可以使用while :( :(也难,因为我们不能使用任何我们没有看到的选项...
$ b $所以,我有3个问题:

$ ul

  • 我如何做一个适当的无限循环?

  • 如何才能完成它?

  • 如何才能保存值?

  • ul>

    非常感谢所有愿意帮助的人,并为我的无知感到遗憾。

    我是新来的社区,我的问题,问题格式或任何东西都很受欢迎:)

    编辑:我和我的老师说话,他允许我使用 itertools 或者只是一个足够大的范围而不能被达到。现在我想知道,如何将这些值保存在 for 中以便以后操作? 解决方案如果你不允许使用 itertools ,那么你只能使用基本的语言结构,而不能使用,而那么我对你有一些不幸的消息;



    这可能是不可能的。至少不要用python for 循环。



    我建议你与领导班级的人让他们澄清要求。这个知识没有现实世界的应用,所以我会非常有兴趣知道这个任务的目标是什么。

    注意;这个在其他支持实际循环的语言中是可能的(也就是说,而不是foreach循环),所以这个任务的分配者可能从不同的语言类中移植了这个问题?






    编辑;现在,你可以使用itertools,我会建议使用我原来在评论中给出的答案;

      from itertools import计数
    为我计数():
    通过#这将循环更长的时间比你活着。

    通过这个,加上在循环之前创建的列表,您应该没有难度跟踪成绩和计算其平均值。

    I'm learning Python (using 3.6.2) and on my last class, they asked me to do something where I need to make an infinite for loop. For some reason, the teacher doesn't want us to use while for the entire practice. This is where it gets complicated...

    So, I've been looking for a way to do it. But, it's also difficult because the teacher doesn't want us to use any commands we haven't seen in class. So I can't use .append, sys functions, well, I can't even use a break. I must find a way to do with "simple" commands.

    I thought I could do it this way;

    x=1
        for i in range(x):
        do_something()
        x += 1
    

    However, it didn't seemed to work. I think that's because Python doesn't read the value for the range again?

    I couldn't find a way, but after hours of thinking I found myself a small workaround I could use:

    def ex():
        print("Welcome")
        for i in range(1):
            math = int(input("Please insert grades obtained at Math, or insert 666 to exit" ))
            if(math > 0 and math < 60):
                print("Sorry. You failed the test")
                return ex():
            elif(math >= 60 and math <= 100):
                print("Congratulations. You passed the test")
                return ex():
            elif(math == 666):
                return exit()
            else:
                print("ERROR: Please insert a valid number")
                return ex():
    
    def exit():
         pass
    

    As you can see, what makes it "infinite" is that it returns to the function once and once again, until you tell the program to "exit", by entering "666". I'd also like to have a more proper way to exit the function.

    I'm still wondering if there's a better way to make my for loop infinite until the user calls it to stop. However, one way or another I got this exercise working. The problem came when I started with the second one, which is more or less like this:

    Imagine the same past program, but this time, it will not just show you if you passed the test or not. It wants to collect as many grades you enter through the input, and then calculate the average of all the grades. I'm not able to save those values (the grades) because I kind of "restart" my own function every time.

    And according to my teacher's instructions, I can't ask the user how many grades he wants me to calculate. It must be infinite and keep asking for inputs until the user choses not to.

    I'm really stuck and lost on it. It's very hard and frustrating because it'd be way easier if we could just use while's :( And also harder because we can't use any options we haven't seen...

    So, I have 3 questions:

    • How do I make an appropiate "infinite" for loop?
    • How do I make a proper way to "finish" it?
    • How do I make it able to save values?

    A lot of thanks in advance for anyone willing to help, and sorry for my ignorance.
    I'm new to the community, so any advice about my problems, the question formatting or anything is well received :)

    EDIT: I talked to my teacher and he allowed me to use either itertools or just a range big enough to not be reached. Now i'm wondering, how can I save those values inside the for for later manipulation?

    解决方案

    If you are not allowed to use itertools and you are limited to basic language constructs, and not allowed to use while then I have some sad news for you;

    This might not be possible. At least not with a python for loop.

    I suggest you get in contact with whoever is leading your class and get them to clarify the requirements. There is no real-world application for this knowledge, so I would be very interested to know what the goal of this assignment is.

    Note; This is possible in other languages that support actual for loops (ie, rather than foreach loops) so maybe the assigner of this task ported this question from a different language class?


    EDIT; Now that you're allowed to use itertools, I would suggest using the answer I gave in the comments originally;

    from itertools import count
    for i in count():
        pass  # this will loop for longer than you will live.
    

    With this, plus a list created before the loop, you should have no difficulty keeping track of grades and calculating their average.

    这篇关于我怎么能做一个无限的“为”循环能够保存值,而不使用“while”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆