python结果怎么保存,如何保存来自“for”的值循环? Python 3

Yesterday I posted a question where I was searching for a way to do an infinite for loop, without using while at all (because my teacher wants so, and also, we can't use any commands we haven't seen in class). It was difficult as apparently there wasn't a very viable option that didn't use while, or other functions like itertools or .append, etc.

You can see that question here

Also, thanks a lot for the feedback you guys brought me! :)

But I managed to talk with my teacher and we got permission to use itertools or just a range big enough (instead of actually infinite).

I solved a few exercises already, but now I have the following instructions:

(Think about grades)

• Ask the user a number (through inputs), and keep asking until the user tells to stop.

• Then, calculate the average from all the numbers entered.

(It's actually a little more complex, but I shortened it and I believe I can deal with the rest)

As I said, I must use a for loop, and I can't use whiles at all.

If I could use while, I'd do something like this:

def grades():

totalg = 0

countg = 0

keepAdding = "y"

while(keepAdding == "y"):

qualif = int(input("Insert grades obtained, in scale from 0 to 100 "))

totalg = totalg + qualif

countg = countg + 1

keepAdding = str(input("Do you wish to keep adding data? (y/n) "))

print("The average of your grades is", totalg/countg)

How can I do something like that, but with for loops? I have no idea on how to store the data for later calculation.

Also, I'm interested into knowing a more "proper" way to be able to end the loop, but I can't use a break neither.

Thanks in advance! Any advice is appreciated and welcome! :)

解决方案

One way to do this is, without outside modules, is to use two-arg iter; when passed two arguments, the first is a no-argument function to call over and over, and the second is a sentinel value than indicates you should stop.

So for example, you could make an infinite loop with something as simple as:

for _ in iter(bool, True):

Since the bool constructor returns False, a sentinel value of True will never occur, so that's an infinite loop.

Similarly, to make a loop that prompts until the user responds with a given sentinel string, like 'q' for quit (or just the empty string), you could do:

for inp in iter(lambda: input("Insert grades obtained, in scale from 0 to 100 (type 'q' to quit)"), 'q'):

val = int(inp)

... rest of loop ...

Obviously, this is a little obscure (two-arg iter is rarely seen), so usually you'd use while True: with the loop containing a test-and-break or test-and-return (the latter doesn't violate the letter of the teacher's requirements). Similarly, exception handling can be used to exit the loop, though it's ugly:

try:

for ...:

if test_for_end:

raise StopIteration

except StopIteration:

pass

# You're outside the loop

Note: Literally everything I've mentioned is terrible style, aside from an actual while True: loop with a test-and-break/return case. But you've got one hand tied behind your back, so I'm suggesting some terrible mutant hands to substitute.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值