What is the best way to implement a reusable usr confirmation?

Basically so that I can embed it in other functions, so that the user will get to confirm if output is ok or whether they want to recall function or quit.

So it seems it should be like.

def userConfirm():
    """get user confirmation to proceed"""
    userConfirm = input("Are you happy with results? (Y or N or Q): ")
    if userConfirm() == 'N':
        #Call some function again
    elif userConfirm() == 'Q':
        #terminate program
    else:
        pass

2 questions.
1) How do I call the functions if I don't know what they are?
2) what 'return' do I use for my function if the logic is complete?

My issue is that I am getting name errors for the Y N & Q.

def userConfirm(someFunc):
    """get user confirmation to proceed"""
    userConfirm = input("Are you happy with results? (Y or N or Q): ")
    if userConfirm() == 'N':
        someFunc()
    elif userConfirm() == 'Q':
        sys.exit("You Quit the Program")
    else:
        pass

Can you post the output?

The problem might be that you define userConfirm as both a function (top level), as a variable (line 3) and then call it as a function again in lines 4 and 6.

You also never need lines 8/9 since "else: pass" is the same as dropping those lines completely.

I have since changed it but still not working

import sys


def userConfirm():
    """get user confirmation to proceed"""
    Y = ''
    N = ''
    Q = ''
    userChoice = input("Are you happy with results? (Y or N or Q): ")
    if userChoice() == N:
        pass
    elif userChoice() == Q:
        sys.exit("You Quit the Program")
    elif userChoice == Y:
        print("OK Proceeding")




>>> from testConfirm import userConfirm
>>> userConfirm()
Are you happy with results? (Y or N or Q): Q
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "testConfirm.py", line 10, in userConfirm
    if userChoice() == N:
TypeError: 'str' object is not callable
>>> 

OK solved it.

import sys


def userConfirm():
    """get user confirmation to proceed"""
    userChoice = raw_input("Are you happy with results? (Y or N or Q): ")
    if userChoice == "N":
        pass
    elif userChoice == "Q":
        sys.exit("You Quit the Program")
    elif userChoice == "Y":
        print("OK Proceeding")

You can probably drop lines 7/8, no?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.