循环时从Python中获得奇怪而意外的输出
我简单地循环增加了一个数字。然后,我做了一个完全分开的条件,如果在某些情况下打印声明。我不明白为什么两个人都融合在一起.....
编写一个程序,其输入是两个整数。输出第一个整数 只要值小于或 等于第二个整数。
ex:如果输入为:
-15
10
输出为:
-15 -10 -5 0 5 10
ex:如果第二个整数小于第一个整数,则如下:
20
5
输出为:
Second integer can't be less than the first.
用于编码简单性,在每个整数之后输出一个空间,包括 最后一个。
我的代码:
''' Type your code here. '''
firstNum = int(input())
secondNum = int(input())
while firstNum <= secondNum:
print(firstNum, end=" ")
firstNum +=5
if firstNum > secondNum:
print("Second integer can't be less than the first.")
输入程序输入(可选)
-15
10
在此处显示的程序输出
-15 -10 -5 0 5 10 Second integer can't be less than the first.
I made a simple while loop to increase a number. And then I made a completely separate if condition to print a statement under certain circumstances. I don't understand why the two are being joined together.....
Write a program whose input is two integers. Output the first integer
and subsequent increments of 5 as long as the value is less than or
equal to the second integer.Ex: If the input is:
-15
10
the output is:
-15 -10 -5 0 5 10
Ex: If the second integer is less than the first as in:
20
5
the output is:
Second integer can't be less than the first.
For coding simplicity, output a space after every integer, including
the last.
My code:
''' Type your code here. '''
firstNum = int(input())
secondNum = int(input())
while firstNum <= secondNum:
print(firstNum, end=" ")
firstNum +=5
if firstNum > secondNum:
print("Second integer can't be less than the first.")
Enter program input (optional)
-15
10
Program output displayed here
-15 -10 -5 0 5 10 Second integer can't be less than the first.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
循环确保
firstNum&gt;第二名
在完成运行时。然后,您检查是否FirstNum&gt; secondnum
(它是),您的print
语句被执行。Your
while
loop is ensuringfirstNum > secondNum
by the time it finishes running. Then, you check to see iffirstNum > secondNum
(which it is), and yourprint
statement gets executed.