Writing An Infinite While Loop That Asks For User Input Until They Specify To Stop

13 September 2019 - 1 answer

I'm creating an infinite while loop that counts up or down between two user inputted numbers. When it reaches the end of the count, the user is asked "Would you like to start over? Enter Y to start over or N to exit:"

The full assignment is here -

1. The first prompt should be "Enter the starting number: "

2. The second prompt should be "Enter the ending number: "

3. The third prompt should be "Enter U to count up or D to count down: "

4. The program should then print each number from the starting number to the ending number (inclusive), each on a new line, going up or down depending on what the user entered

5. After printing the final number, the program should prompt the user "Would you like to start over? Enter Y to start over or N to exit: "

6. If the user enters Y, the program should start again with the first prompt. Otherwise, it should print "Goodbye!" and exit.

7. The prompts must be case insensitive, that is, the user should be able to enter Y or y, N or n, U or u, D or d

connection = True

``````while connection:
starting_number = int(input("Enter the starting number:\n"))
end_number = int(input("Enter the ending number:\n"))
the_count = input("Enter U to count up or D to count down:\n")
if the_count.upper() != "U":
for num in range(starting_number, end_number + 1):
if connection == end_number:
repeat_count = input("Would you like to start over? Enter Y to start over or N to exit:\n")
if repeat_count.upper() != "Y":
print(connection)
if repeat_count.upper() != "N":
print("Goodbye!")
break
if the_count.upper() != "D":
for num in range(end_number, starting_number - 1):
if connection == starting_number:
repeat_count = input("Would you like to start over? Enter Y to start over or N to exit:\n")
if repeat_count.upper() != "Y":
print(connection)
if repeat_count.upper() != "N":
print("Goodbye!")
break
else:
print("Goodbye!")
break
print()
``````

After inputting the starting and end number, you are prompted to press U to count up or D to count down. If you type U, the prompt to enter first number appears again. If you type D, the function prints "Goodbye!".

Any help at all would be great! Thanks

Edit: Having looked at the answer here - Asking the user for input until they give a valid response I disagree that it is a duplicate. My function accepts user input and does not crash based on a mistake.

The reason for this is that your second `if` executes regardless of what the first `if` did. So if you type `D`, you execute the code in the first `if` (because `D != U`), and then you also execute the `else:` block of the second `if`.

You need to `elif:` so the conditions are all mutually exclusive.

Also, you seem to be using `!=` when you should be using `==`.

The instructions say to ask the user whether they want to start over after you print the last number. You're asking them every time through the loop if `connection` is equal to the last number to be printed. I don't know what `connection` is, but it doesn't seem to have anything to do with the assignment.

You should take that question out of the `if` blocks, since it doesn't need to be inside the loops that print the numbers. It's the same question no matter which direction they're going.

When counting down, you need to provide the step value `-1` in the `range()` function. It doesn't automatically switch to decreasing when the starting number is higher than the ending number.

``````while True:
starting_number = int(input("Enter the starting number:\n"))
end_number = int(input("Enter the ending number:\n"))
the_count = input("Enter U to count up or D to count down:\n")
if the_count.upper() == "U":
for num in range(starting_number, end_number + 1):
print(num)
elif the_count.upper() == "D":
for num in range(end_number, starting_number - 1, -1):
print(num)
else:
print("Goodbye!")
break
repeat_count = input("Would you like to start over? Enter Y to start over or N to exit: ")
if repeat_count.upper() != 'Y':
print("Goodbye!")
break
print()
``````