我有以下代码:
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‘,但是循环是无限的,所以它永远不会中断。
发布于 2019-11-21 00:02:07
您可以使用此方法,只需循环一次,如果计数器可除数为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
10https://stackoverflow.com/questions/58965143
复制相似问题