查找带有while as的偶数

2024-04-20 02:23:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做这个任务:

Write a program that prints all even numbers less than the input number using the while loop.

The input format:

The maximum number N that varies from 1 to 200.

The output format:

All even numbers less than N in ascending order. Each number must be on a separate line.

N = int(input())
i = 0
while 200 >= N >= 1:
    i += 1
    if i % 2 == 0 and N > i:
        print(i)

其输出如下:

10  # this is my input
2
4
6
8

但是关于时间的问题有一个错误


Tags: theformatnumberinputthatallprogramprints
2条回答

简单的代码是:

import math

N = int(input(""))
print("1. " + str(N))

num = 1

while num < math.ceil(N/2):
    print (str(num) + ". " + str(num * 2))
    num += 1

问题是while循环从不停止

while 200 >= N >= 1在这种情况下,由于从不更改N的值,因此条件始终为true。也许你可以做更多类似的事情:

N = int(input())
if N > 0 and N <= 200:
    i = 0
    while i < N:
        i += 2
        print(i)
else 
    print("The input can only be a number from 1 to 200")

相关问题 更多 >