0

So basically when a user enters 'n' I need it to stop and not ask to enter either y or n, but I can't seem to make it occur, any help?

decider = input('Would you like to play the game? [y/n]? ')

while decider != 'y' :
    if decider == 'n' :
        print('Another time perhaps')
    else :
        print('Please enter either \'y\' or \'n\' ')

    decider = input('Would you like to play the game? [y/n]? ')

Also I understand you can use exit or break but in my exercise that is NOT allowed

5
  • 1
    Instead of asking at the end of the loop, have you tried asking at the beginning? Consider what you'd need to change in your program, if you did that. Commented Aug 20, 2020 at 13:07
  • @GreenCloakGuy I tried putting it at the beginning inside the while loop, but it does the same thing :( Commented Aug 20, 2020 at 13:18
  • 1
    Please don't vandalize your own posts. When you post here, you give SO the right to distribute the content under CC-by SA 4.0. Any vandalism will be reverted. Commented Aug 23, 2020 at 11:09
  • @greg-449 Just wanted to delete since I got my solution and didn't want people to keep helping out, understood! thanks. Commented Aug 25, 2020 at 11:43
  • Questions are not just for the person asking but for all the other people who might have a similar problem. So you should not try to delete just because you got an answer. Commented Aug 25, 2020 at 12:25

3 Answers 3

1

You need to ask the user before checking the inputs and before the while loop checks the condition again. Also you need to loop as long as the input is NOT 'n'

decider = ''
while decider != 'n' and decide != 'y' :
    decider = input('Would you like to play the game? [y/n]? ')
    if decider == 'n' :
        print('Another time perhaps')
        break
    elif decider == 'y':
        print('Play again!')
        * insert restart code here*
    else :
        print('Please enter either \'y\' or \'n\' ')
Sign up to request clarification or add additional context in comments.

7 Comments

Hmm this doesn't seem to do it, the program would not pass through even if it's a 'y'
decider = ' ' while decider != 'y' : decider = input('Would you like to play the game? [y/n]? ') if decider == 'n' : print('Another time perhaps') else : print('Please enter either \'y\' or \'n\' ') I tried this, which is close to working however when I put an 'n' it still asks the question
Are you trying to exit the loop when the user presses 'y' or rather at 'n'?
Moray it needs to exit at n, and not ask again when it comes to that, and at 'y' it would just continue to the rest of my program
Okay, see my changes.
|
0

Try this:

run = True
while run:
    decider = input(•••)
    if decider == "n":
        print(•••)
        run = False
    if decider == "y":
        print(•••)
    else:
        print("Please write either y or n")

5 Comments

I'm not allowed to use 'break' for this exercise unfortunately, any other solution?
Alright, I'll edit the answer to avoid using break
@Danyal Khan check it now, is the answer satisfactory? If it is, please mark it as accepted 😃
Nope it still has the same issue, in this case when a user inputs 'y' it will pass through, when a user inputs 'n' it should stop, and when a user inputs anything other than y or n, it should ask them to input either from the two
@DanyalKhan Okay, check it now
0

You can solve it adding other constring to the while ald change it when he inset n. I gave you and example below.

import sys

decider = input('Would you like to play the game? [y/n]? ')

while decider != 'y':
    if decider == 'n' :
        print('Another time perhaps')
        sys.exit()
    else :
        print('Please enter either \'y\' or \'n\' ')
        decider = input('Would you like to play the game? [y/n]? ')

Well you were rigth I edit the code to not answer the question again

3 Comments

Hi Andres this seems to work for 'y' and anything that is not 'n', however when 'n' is entered and submitted as a response it still asks the question and on the second attempt it passes through to the second part of my program ...
Andres this is perfect, it works on solo but my program has two parts: the while loop we are talking about and then another while loop. The thing is I need the loop to not continue to the second part if the user inputs 'n' , any help?
Okay i finaly understand what you want. i had update the changes now. Now if the user insert an n the program will finish

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.