简体   繁体   English

无法跳出while循环

[英]Not able to break out of while loop

I have the following code:我有以下代码:

with open('data.csv') as csvfile:
    data = csv.reader(csvfile, delimiter=' ')
    row_count = sum(1 for row in data)
    counter = 0
    while counter < row_count:
        for i in range(20):
            print('hello')
        counter = counter +1
        print('sleeping')
        time.sleep(10)

The data.csv file have over 100 lines. data.csv文件有 100 多行。 But I want my counter to print only 20 lines of hello and then sleep for 10 seconds and restart the operation again until it matches the number in row_count .但我希望我的计数器只打印 20 行 hello 然后休眠 10 秒并再次重新启动操作,直到它与row_count中的数字匹配。

The output is fine in that it prints hello 20 times and prints sleep with a delay but this loop does not end when it reaches row_count which is 100. This keeps on running. output 很好,它打印 hello 20 次并延迟打印 sleep,但是当达到row_count为 100 时,此循环不会结束。这会继续运行。 What can I do for this loop to end after it reaches 100?我该怎么做才能让这个循环在达到 100 后结束?

Your identation is incorrect.你的识别不正确。 If you ident the line where you increment the counter it will do what you want.如果您确定增加计数器的行,它将执行您想要的操作。 Currently you are doing 100 (or whatever row_number equals) lots of 20.目前你正在做 100 个(或任何 row_number 等于)很多 20。

EDIT: I wrote this thinking that you wanted to print "Hello" row_number times, but reading your post back it seems that you actually want to print "Hello" row_number * 20 times, in which case surely 2000 is the correct answer?编辑:我写这篇文章的想法是你想打印“Hello” row_number次,但读回你的帖子似乎你实际上想要打印“Hello” row_number * 20次,在这种情况下,2000 肯定是正确的答案吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM