从 python while 循环中得到奇怪和意外的输出

Getting strange and unexpected output from python while loop

我做了一个简单的 while 循环来增加一个数字。然后我做了一个完全独立的 if 条件来在某些情况下打印语句。不明白为什么要把两者连在一起.....

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.

我的代码:

''' 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.

您的 while 循环确保 firstNum > secondNum 到它完成时 运行。然后,您检查是否 firstNum > secondNum (它是),并且您的 print 语句被执行。