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 :
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