Search code examples
pythonmethodstry-catchprogram-entry-pointexcept

How do I create a function/method out of this code and then call it to the main()?


enter code hereThis is the original code that I wrote:

while True:
    user_input = (input(">>",))
    try:
        user_input = int(user_input)
    except ValueError:
        pass
    if user_input in range(1, len(something):
        break

I want to put in a method:

`get_user_answer(n: int, p: str) -> int` # This is what the method should look like kind of
   #But what should I write here?



def main()


#  and how would I call it?

main()

I'm learning about methods so I'm confused

I'm expecting it to work like the code I first wrote, but in a method instead that I call to the main function.


Solution

  • # get user answer function
    def get_user_answer():
        while True:
            user_input = (input(">>",))
            try:
                user_input = int(user_input)
            except ValueError:
                pass
            if user_input in range(1, len(something):
                break
    
    # main function 
    def main():
        # calling get_user_answer function here
        get_user_answer()
    
    #calling main function here
    main()