Python Forum

Full Version: Need help with simple calculator.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
def main():
    choice = input("Add or Sub? ")
    if choice == "Add":
        Add1()
    else:
        Sub1()

def Add1():
    addchoice = str(input("Pick a number. "))
    addchoice1 = str(input("Pick another number. "))
    addconv = int(addchoice) + int(addchoice1)
    addconv1 = str(addconv)
    print("Your answer is:" + " " + addconv1)
    exitcom()
    
def Sub1():
    subchoice = str(input("Pick a number. "))
    subchoice1 = str(input("Pick another number. "))
    subconv = int(subchoice) - int(subchoice1)
    subconv1 = str(subconv)
    print("Your answer is:" + " " + subconv1)
    main()
    
def exitcom():
    exitcom1 = input("Would you like to make another calculation? (Y/N) --> ")
    if exitcom1 == "Y" or "y":
        main()
    if exitcom1 == "n" or "N":
        exit()
        
main()
When running exitcom(), no matter if I type "y" or "n" I'm always returned back to main(). I want it to kill the program when n is type.
Thank you good sir :)
Lay it out this way
def Add1():
    addchoice = input("Pick a number. ")
    print("addchoice is a", type(addchoice))
    addchoice1 = input("Pick another number. ")
    addconv = int(addchoice) + int(addchoice1)
##    addconv1 = str(addconv)
    print("Your answer is: ", addconv)
##    exitcom()

def Sub1():
    subchoice = input("Pick a number. ")
    subchoice1 = input("Pick another number. ")
    subconv = int(subchoice) - int(subchoice1)
##    subconv1 = str(subconv)
    print("Your answer is: ", subconv)
##    main()

again="y"
while again.lower() == "y":
    choice = input("Add or Sub? ")
    if choice in ("Add", "Sub"):
        if choice == "Add":
            Add1()
        else:
            Sub1()
    else:
        print("Not a valid option")
    again=input("Would you like to play again? (y or n) ")