the question is - Please write a program which asks the user to type in a number. The program then prints out the positive integers between 1 and the number itself, alternating between the two ends of the range
my code-
num = int(input('Please type in a number:'))
index = 1
while index <= num:
print(index)
print(num)
index += 1
num -= 1
the program is supposed to this with user input 5-
Please type in a number: 5
1
5
2
4
3
but it instead prints out this
Please type in a number:5
1
5
2
4
3
3
it prints out an extra 3 at the end, how can i fix this?
num = int(input('Please type in a number:'))
index = 1
while index < (num-1):
print(index)
print(num)
index += 1
num -= 1
print(num)
if index < num:
print(index)
Having index < (num - 1) prevents index and num from converging. Printing (num) after the while loop takes care of the median number (3 in the case of 5 like you mentioned). If the number is even, the median will be in between 2 numbers, and The if statement at the end allows to print the smaller of those two