-1

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.

pc90
  • 25
  • 9
  • 2
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – SpghttCd Sep 13 '19 at 08:05
  • What is `connection`? Why are you comparing it to `end_number`? – Barmar Sep 13 '19 at 08:14

1 Answers1

0

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()
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks @Barmar for the help. I see what you mean about checking if it's not something. I had gotten some advice about writing it that way but how you''ve explained it makes sense. I've put in an elif statement but now when I type U or D, it just asks for starting number all over again. Thanks again for your help! Also, sorry, just saw your comment about why am I comparing to connection - that is to prompt repeat_count. Is there a more effective way to do this? – pc90 Sep 13 '19 at 08:28
  • Comparing to connection wouldn't work at all. Where do you set it, and why would it be equal to `starting_number` or `end_number`? And none of those things change during any of the loops, so the result of the test will be the same for every number. – Barmar Sep 13 '19 at 08:35
  • I showed the more effective way to prompt for repeat_count. – Barmar Sep 13 '19 at 08:36
  • Thanks Barmar! New to this so really appreciative of you taking the time to help. – pc90 Sep 13 '19 at 08:39