我有以下代码:
for i in range(10):
while True:
num = int(input("Enter an integer: "))
print("The double of",num,"is",2 * num)
print('10')
我想做的是经过10次迭代后,消息应该显示10。它只是一次,我如何重置计数器,以便计数器达到10后会再次启动?
我希望程序执行的操作是在10次迭代后打印'10',但是循环是无限的,因此它永远不会中断。
答案 0 :(得分:1)
您可以使用它,只循环一次,然后检查计数器是否可以除以10以打印消息
for i in range(1, 100):
num = int(input("Enter an integer: "))
print("The double of",num,"is",2 * num)
if i%10==0:
print('10')
如果您需要无限循环:
i = 1
while True:
num = int(input("Enter an integer: "))
print("The double of",num,"is",2 * num)
if i%10==0:
print('10')
i+=1
for i in range(1,21)
的结果将是
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
10
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
10