Chris Howard
Chris Howard

Reputation: 9

python immediately closing when I open program

I will admit i am just starting out python, but i seem to be having a problem. I created this calculator program and when i open it, the python window pops up for half a second than goes away. Does anybody have any idea why? Is it possibly a syntax error or something wrong with python? I am running windows 10 and the most recent version of python. all of my code is below, thanks.

def multiply():
    print("Choose two numbers to multiply by")
    A = int(input("Enter A: "))
    B = int(input("Enter B: "))
    return A * B

def divide():
    print("Choose two numbers to divide by")
    A = int(input("Enter A: "))
    B = int(input("Enter B: "))
    return A / B

def subtract():
    print("Choose two numbers to divide by")
    A = int(input("Enter A"))
    B = int(input("Enter B"))
    return A - B

def add():
    print("Choose two numbers to divide by")
    A = int(input("Enter A"))
    B = int(input("Enter B"))
    return A + B

def squareRoot():
    print("Choose what number to take the square root of")
    A = int(input("Enter A"))
    return A ** .5

def Square():
    print ("Choose what number to square")
    A = int(input("Enter A"))
    return A **

Print ("1: Addition")
Print ("2: Subtract")
Print ("3: Multiply")
Print ("4: Divide")
Print ("5: Square Root")
Print ("6: Square")
Print ("7: QUIT")

while True:
    Choice = int(input("Enter the corresponding number for calculation"))

    if Choice == 1:
        print ("Adding two numbers")
        print add()

    elif Choice == 2:
        print("Subtracting numbers")
        print subtract()

    elif Choice == 3:
        print("Multiplying numbers")
        print multiply()

    elif Choice == 4:
        print("Dividing numbers")
        print divide()

    elif Choice == 5:
        print("Taking the square root, your majesty")
        print squareRoot()

    elif Choice == 6:
        print("Squaring your god damned number")
        print squareRoot()

    elif Choice == 7:
        exit()

    else:
        Print("You didnt choose one of the options")

Upvotes: 0

Views: 65

Answers (1)

mhawke
mhawke

Reputation: 87054

Python starts up OK and begins running the script, but there is an error in Square() that raises an unhandled SyntaxError exception. The error causes the script to terminate and that closes the window. The return statement in Square() should be :

return A * A

not:

return A **

The next error is that you are calling Print() instead of print(). You are using Python 3, but there are other print statements which should actually be calls to the print function, i.e. print(), example line 48.

Also, there is a bug where option 6 calls squareRoot() instead of Square().

The best way for you to find all of these errors is to execute your script from the command line of a windows terminal.

Upvotes: 2

Related Questions