Jettobi

Program did not run correctly

Recommended Posts

Hello everyone! I was watching kjdElectronics youtube videos and was looking at his tutorial 7 video. And I was following his coding until this part of the video where he managed to get his calculator program working. But when I try to do it myself, the program did not work as it should. Here are my codings:

def add(num1, num2):
    return num1 + num2


def sub(num1, num2):
    return num1 - num2


def mul(num1, num2):
    return num1 * num2


def div(num1, num2):
    try:
        return num1 / num2
    except ZeroDivisionError:
        print("Error, you cannot divide by zero")


def main():
    user_continue = True
    while user_continue:
        validInput = False
        while not validInput:
            try:
                myName = input("What is your name?")
                print("Hello")
                print(myName)
                num1 = int(input("Enter num1:"))
                num2 = int(input("Enter num2:"))
                operation = input("What do you want to do?(1.addition, 2.subtraction, 3.multiply, 4.division)")
                validInput = True
            except:
                print ("Invalid Command, please try again!")
        if (operation == '1'):
            print("answer:")
            print(add(num1, num2))
        elif (operation == '2'):
            print("answer:")
            print(sub(num1,num2))
        elif (operation == '3'):
            print("answer:")
            print(mul(num1, num2))
        elif (operation == '4'):
            print("answer:")
            print(div(num1, num2))
        else:
            print("Invalid")

        user_yn = input('Would you like to run another calculation? (Y/N)')
        if (user_yn != 'Y'):
            user_continue = False
            break
        else:
            continue

And when I run it, I got this:

Process finished with exit code 0

Rather than asking me to input my name or num1 and num2. Can anyone help me spot any error in my codings? That will be greatly appreciated! Thanks in advance:)

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...