循环时从Python中获得奇怪而意外的输出

发布于 2025-01-25 12:28:25 字数 1038 浏览 3 评论 0原文

我简单地循环增加了一个数字。然后,我做了一个完全分开的条件,如果在某些情况下打印声明。我不明白为什么两个人都融合在一起.....

编写一个程序,其输入是两个整数。输出第一个整数 只要值小于或 等于第二个整数。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

决绝 2025-02-01 12:28:25

您的循环确保firstNum&gt;第二名在完成运行时。然后,您检查是否FirstNum&gt; secondnum(它是),您的print语句被执行。

Your while loop is ensuring firstNum > secondNum by the time it finishes running. Then, you check to see if firstNum > secondNum (which it is), and your print statement gets executed.

明明#如月 2025-02-01 12:28:25
a = int(input())
b = int(input())
if b < a:
    print("Second integer can't be less than the first.")
else:
    while a <= b:
        print(a, end=" ")
        a = a + 5
    print("")
a = int(input())
b = int(input())
if b < a:
    print("Second integer can't be less than the first.")
else:
    while a <= b:
        print(a, end=" ")
        a = a + 5
    print("")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文