I'm very new to Python, and so I'm trying to learn it by doing simple programs, like a calculator. But I don't know if this is the "Pythonic" way to do it, or if I am making beginner errors.
So could someone please tell if something is wrong with it, and how to improve it?
Here is some sample input and output:
Enter the first number: 2 Enter the second number: 10 Enter an operator (valid operators are +, -, *, / and pow): pow 1024.0
Here is the code
#A basic calculator which supports +, -, *, / and pow def add(a, b): return a + b def sub(a, b): return a - b def mul(a, b): return a * b def div(a, b): return a / b def pow(a, b): return a ** b if __name__ == "__main__": while True: try: number1 = float(input("Enter the first number: ")) number2 = float(input("Enter the second number: ")) except: print("That is not a number!") continue operator = input("Enter an operator (valid operators are +, -, *, / and pow): ") #Check if user operator is a valid one if operator == "+": func = add elif operator == "-": func = sub elif operator == "*": func = mul elif operator == "/": func = div elif operator == "pow": func = pow else: print("Invalid operator!") continue #Print result print(func(number1, number2))