【发布时间】:2019-08-07 21:16:00
【问题描述】:
我正在尝试编写一个读取 2 个正整数(m 和 n)的程序,然后仅使用 while 循环打印 m 的前 n 个正整数。
这是原来的问题
用 Python 3.x 语言编写一个程序,读取两个正整数 m 和 n,并打印前 n 个正整数 m的倍数。
代码的输出应该是这样的:
Type a positive integer for m: 9
Type a positive integer for n: 5
The first 5 positive integers multiples of 9 are:
9
18
27
36
45
到目前为止,我已经完成了:
m = int(input("Type a integer for m: "))
n = int(input("Type a integer for n: "))
i = 1
print()
print("The first ",n,"positive integers multiples of ", m," are:")
while i <= n:
m = m * i
print(m)
i = i + 1
我想了解如何解决这个问题,我意识到使用 for 或者这样做会更容易
【问题讨论】:
-
在这种情况下,问题是相当明显的,但以后请描述代码的问题所在。它会抛出异常吗?它会产生不正确的输出吗?没有人想猜测或必须运行代码才能找到答案。另请参阅minimal reproducible example。
-
跟进 Aran-Fey 的评论。你得到什么输出?
-
您可能还想考虑如果我为 n 输入“-15”会发生什么
-
或者如果我输入
"fifteen"和"twentytwo"- 请参阅 asking-the-user-for-input-until-they-give-a-valid-response
标签: python python-3.x while-loop