I am attempting to learn Python, to add to my collection of languages and to get at least one programming language under my belt.

So far I've only done the basics, and to test what I've learnt I made an extremelly simple calculator:

#PyCalc Version 1.0

Loop = 0

print("-------------------------------------")
print("Welcome to the PyCalc Calculator V1.0")
print("-------------------------------------")

def Menu():

    print("\n\n1 - Addition\t\t (+)")
    print("2 - Subtraction\t\t (-)")
    print("3 - Division\t\t (/)")
    print("4 - Multiplication\t (*)")
    print("5 - Exit")

    return input("\n\nPlease make your selection: ")

def Addition(Val1, Val2):
    print("\n\n\t", Val1, " + ", Val2, " = ", Val1 + Val2)

def Subtraction(Val1, Val2):
    print("\n\n\t", Val1, " - ", Val2, " = ", Val1 - Val2)

def Division(Val1, Val2):
    print("\n\n\t", Val1, " / ", Val2, " = ", Val1 / Val2)

def Multiplication(Val1, Val2):
    print("\n\n\t", Val1, " * ", Val2, " = ", Val1 * Val2)


while Loop == 0:

    Selection = Menu()

    if Selection == "1":
        Addition(int(input("\n\nX: ")), int(input("Y: ")))

    elif Selection == "2":
        Subtraction(int(input("\n\nX: ")), int(input("Y: ")))

    elif Selection == "3":
        Division(int(input("\n\nX: ")), int(input("Y: ")))

    elif Selection == "4":
        Multiplication(int(input("\n\nX: ")), int(input("Y: ")))

    elif Selection == "5":
        print("\n\nThank you for using the PyCalc Calculator V1.0")
        Loop = 1

    else:
        print("\n\nInvalid Selection")

The code works, but I don't know if it is the most efficient, or if I've done it in the best way?
Can you see any issues and is there anything I can do to improve it?

Thank you

Here is some possibilities. At least do not use the capitalized names traditionally used for class names for simple instance variables. Also avoid unnecessary empty lines, which in most cases makes code less not more readable.

#PyCalc Version 1.0
#for Python2
from __future__ import print_function, division
try:
    input = raw_input
except:
    pass
from operator import add, sub, mul, truediv

actions = [(add, '+'), (sub, '-'),  (truediv, '/'), (mul, '*')]


def menu():
    return input("""
1 - Addition\t\t (+)
2 - Subtraction\t\t (-)
3 - Division\t\t (/)
4 - Multiplication\t (*)
5 - Exit

Please make your selection: """)


def get_nums(t=float):
    try:
        return t(input("\n\nX: ")), t(input("Y: "))
    except Exception:
        return None, None

def show(action, val1, val2):
    try:
        print("\n\n\t", val1, action[1], val2, " = ",action[0](val1, val2))
    except:
        print("Error in input")


print("""
-------------------------------------
Welcome to the PyCalc Calculator V1.0
-------------------------------------
""")
while True:
    try:
        selection = int(menu())
        if not (0 < selection <= 5):
            raise ValueError('Out of range')
    except Exception:
        print("\n\nInvalid selection")
    else:
        if selection == 5:
            break
        else:
            print('\nArguments for', repr(actions[selection-1][1]))
            show(actions[selection-1], *get_nums())


print("\n\nThank you for using the PyCalc Calculator V1.0")

Edited 3 Years Ago by pyTony

Thanks for the tips pyTony, they've proven to be really useful!

This question has already been answered. Start a new discussion instead.