2

I am looking for a way to implement a "press Y to continue, N to cancel" prompt.

My current way to implement it is

Prompt = None
# Loop until the user inputs a valid answer
while Prompt not in ("yes", "y", "n", "no"):
    Prompt = input("Do you wish to continue? answer y or n\n")
    if Prompt == 'y' or == 'yes':
        state = 2 # switch state to processing state
    elif Prompt == 'n' or == 'no': # cancel
        break

Are there any more efficient ways of implementing this prompt?

charlesreid1
  • 4,360
  • 4
  • 30
  • 52
  • Your code has a compile error "if Prompt == 'y' or == 'yes':" change to > "if Prompt == 'y' or Prompt == 'yes': . Same goes for the elif line. –  Dec 22 '19 at 07:03

3 Answers3

2

Yes, simply try the following.

while True:
    Prompt = input("Do you wish to continue? answer y or n\n")
    if Prompt in ['y', 'yes']:
        state = 2 # switch state to processing state
    elif Prompt in ['n', 'no']:
        break
2

What is not efficient about this?

You can retype this code in a lot of ways but none of them have anything to do with efficiency. The bigger question is why you are even concerned about this.

Don't get lost in trying to make things efficient especially if you are a beginner.


Edit

Method 1

If you have a scenario where you have multiple prompts the you could think of getting rid of if statements and setting up an interface like a dictionary that maps each user's prompt response to a function that handles that specific prompt that what you would just have something like

def PrintHello():
    print('Hello')

LookUpFunction = {'hello':PrintHello}

def HandleUser():
    try:
        return LookUpFunction[input('What would you like to do ? ')]
    except KeyError:
        return None


while True:
    func = HandleUser()
    if(func):
        func()
        break

Method2

You could also just create functions that are name aliases for the input string that the user provides and then use locals or globals to call the function like this.

def c_hello():
    print('Hello There')
def c_quit():
    print('Goodbye')


command = locals()['c_'+input('What would you like? ')]
command()

so all you have to do in both this cases is provide the functions that handles the particular Prompt

masonCherry
  • 894
  • 6
  • 14
  • 2
    While it's true that i am aware that it won't impact the efficiency in terms of performance..etc. I am looking for simply the right way to implement it using less lines. Just in case i have to write many prompts –  Dec 22 '19 at 07:24
1

Your code looks wrong, maybe I can help it. Try my code

Prompt = None
while Prompt not in ("yes", "y", "n", "no"): #loop until the user inputs a valid answer
        Prompt = input("Do you wish to continue? answer y or n\n")
        if Prompt in ('y', 'yes'):
            state = 2 # switch state to processing state
        elif Prompt in ('n', 'no'): #cancel
            break

The Prompt == 'y' or == 'yes' and Prompt == 'n' or == 'no': line is incorrect, because it should be Prompt == 'y' or Prompt == 'yes' and Prompt == 'n' or Prompt == 'no'. But I more prefer using in operator

FaranAiki
  • 453
  • 3
  • 12