Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How would I get the results from this to end at a value of 2?

This is a countdown calculator that shows all even numbers from user input to 2. If the user inputs an odd number, it adds 1 to make the number even. I need it to stop at 2, but I’m not sure how.

startVal = int(input("Enter a starting value:"))
loss = 2
if startVal % 2 == 1:
    startVal = startVal + 1
    for i in range(2, startVal + 1):
        startVal -= loss
        print(startVal)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

all code inside a loop will execute will execute the number of times you specify in the loop condition

> for i in range (0,6):
      print("Hello")




 

would print the word ‘Hello’ 5 times. Please read on the concept of loops.
Meanwhile here is a working solution. (given the input is odd). Use this to figure out how you would do the same logic for an even number.

startVal=int(input("Enter a starting value:"))
loss=2
if startVal%2==1:
    startVal=startVal+1
    temp  = int((startVal/2) - 1)
    for i in range(0,temp):
     startVal-=loss
     print(startVal)

Enter a starting value:17

16

14

12

10

8

6

4

2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading