[Python] Don't know how to use methods created

Discussão em 'Python' iniciado por Stack, Maio 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm super new to Python and programming in general. I am following an example off of youtube on how to make a simple calculator but I wanted to add my ideas and functionalities. More or less I want to make it very versatile.

    This is the part that is not working out for me. I don't know how to make it work using the two methods I created

    def main():
    while True:
    Num1()
    num1 = int.num1()
    Num2()
    num2 = int.num2()


    # Executing the specified calculations with the 'Operation' function (row 13)
    Operation(num1, num2)

    # --> followed by another nested while asking if the user wants to make another calculation


    I want the app to read everywhere that if the user types "exit" it will exit, even if it's asking for a number so I created 2 methods to do so because it wasn't working dealing with the loops and user input directly from main()

    def Num1():
    while True:
    num1 = input("What is the first number? ")
    if num1.isdigit():
    break
    elif num1 == 'exit':
    print("Thank you using the SimpleCalculator. Bye!")
    time.sleep(3)
    exit()
    else:
    print("I don't understand. Please input a valid number")
    continue

    def Num2():

    while True:
    num2 = input("What is the second number? ")
    if num2.isdigit():

    break
    elif num2 == "exit":
    print("Thank you using the SimpleCalculator. Bye!")
    time.sleep(3)
    exit()
    else:
    print("I don't understand. Please input a valid number")
    continue


    The problem is that I get this error and I get it but I just don't know how to go about it.

    Traceback (most recent call last):
    File "C:/Users/Albert/PycharmProjects/HelloWorld - First Py'
    Apps/SimpleCalculator.py", line 131, in <module>
    main()
    File "C:/Users/Albert/PycharmProjects/HelloWorld - First Py'
    Apps/SimpleCalculator.py", line 109, in main
    Operation(num1, num2)
    NameError: name 'num1' is not defined

    Continue reading...

Compartilhe esta Página