0

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.
2
  • 1
    Think about what firstNum would be by the time you get to that if statement. Commented May 2, 2022 at 2:58
  • Thanks. I was under the impression my while loop made it so that the number is only incremented while its less than or equal to the second num. I see that's not the case now.... Commented May 2, 2022 at 3:04

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0
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("")

4 Comments

Please avoid code only answer, which are not as helpful as they could be, with an explanation. Especially on old questions with existing upvoted answers
@chrslg I am very new here. I'll keep in mind.
@FarmitaFariha, please edit your answer to specify what you mean to say with some description. You can still edit your answer any number of times after you post
@FarmitaFariha please don't just "keep it in mind"; edit the answer to add some explanation. NB, I fixed your code. Your version missed an indent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.