如何使用while循环打印2到100的偶数? [英] How is it possible to use a while loop to print even numbers 2 through 100?

查看:533
本文介绍了如何使用while循环打印2到100的偶数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者,因此我陷入了这个问题,编写一个使用while循环打印2到100的偶数的python代码.提示ConsecutiveEven相差2."

I am a beginner and I am stuck on this problem, "Write a python code that uses a while loop to print even numbers from 2 through 100. Hint ConsecutiveEven differ by 2."

这是我到目前为止想出的:

Here is what I came up with so far:

 while num in range(22,101,2):
              print(num)

推荐答案

使用forrange()或使用while并显式增加数字.例如:

Use either for with range(), or use while and explicitly increment the number. For example:

>>> i = 2
>>> while i <=10: # Using while
...    print(i)
...    i += 2
...
2
4
6
8
10

>>> for i in range(2, 11, 2): # Using for
...    print(i)
...
2
4
6
8
10

这篇关于如何使用while循环打印2到100的偶数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆