patternpythonMinor
Python calculator script
Viewed 0 times
scriptcalculatorpython
Problem
Are there any ways to make this calculator script for Python better or simpler?
```
thelist = ["Add", "add", "Multiply", "multiply", "Divide", "divide","Subtract", "subtract"]
def Multiply(x,y):
z = x * y
print(z)
def Divide(x,y):
x = float(x)
y = float(y)
z = x / y
print(z)
def Add(x,y):
z = x + y
print(z)
def Subtract(x,y):
z = x - y
print(z)
while True:
operation = input("What would you like to do? Multiply/Divide/Add/Subtract ")
if operation in thelist:
break
else:
print("That was not an option..")
if operation == "Multiply" or operation == "multiply":
while True:
try:
x = int(input("First number: "))
break
except ValueError:
print("Make sure to enter a number.")
while True:
try:
y = int(input("Second number: "))
break
except ValueError:
print("Make sure to enter a number...")
Multiply(x,y)
elif operation == "subtract" or operation == "Subtract":
while True:
try:
x = int(input("First number: "))
break
except ValueError:
print("Make sure to enter a number.")
while True:
try:
y = int(input("Second number: "))
break
except ValueError:
print("Make sure to enter a number.")
Subtract(x,y)
elif operation == "Add" or operation == "add":
while True:
try:
x = int(input("First number: "))
break
except ValueError:
print("Make sure to enter a number..")
while True:
try:
y = int(input("Second number: "))
break
except ValueError:
print("Make sure to enter a number.")
Add(x,y)
elif operation == "Divide" or operation == "divide":
while True:
try:
x = int(input("First number: "))
break
except ValueError:
print("Make sure to enter a number.")
whi
```
thelist = ["Add", "add", "Multiply", "multiply", "Divide", "divide","Subtract", "subtract"]
def Multiply(x,y):
z = x * y
print(z)
def Divide(x,y):
x = float(x)
y = float(y)
z = x / y
print(z)
def Add(x,y):
z = x + y
print(z)
def Subtract(x,y):
z = x - y
print(z)
while True:
operation = input("What would you like to do? Multiply/Divide/Add/Subtract ")
if operation in thelist:
break
else:
print("That was not an option..")
if operation == "Multiply" or operation == "multiply":
while True:
try:
x = int(input("First number: "))
break
except ValueError:
print("Make sure to enter a number.")
while True:
try:
y = int(input("Second number: "))
break
except ValueError:
print("Make sure to enter a number...")
Multiply(x,y)
elif operation == "subtract" or operation == "Subtract":
while True:
try:
x = int(input("First number: "))
break
except ValueError:
print("Make sure to enter a number.")
while True:
try:
y = int(input("Second number: "))
break
except ValueError:
print("Make sure to enter a number.")
Subtract(x,y)
elif operation == "Add" or operation == "add":
while True:
try:
x = int(input("First number: "))
break
except ValueError:
print("Make sure to enter a number..")
while True:
try:
y = int(input("Second number: "))
break
except ValueError:
print("Make sure to enter a number.")
Add(x,y)
elif operation == "Divide" or operation == "divide":
while True:
try:
x = int(input("First number: "))
break
except ValueError:
print("Make sure to enter a number.")
whi
Solution
A thorough review would very much depend on what you're trying to achieve.
For example, it seams mighty clumsy to have to type
Judging from your use of
in function
Like most (all?) interpreted and pseudocompiled languages, Python has
One thing seems strange to me. You do
Why don't you first input
Regarding your naming of functions, function names should be lowercase, with words separated by underscores as necessary to improve readability, i.e., you shouldn't capitalize the names of your functions.
For example, it seams mighty clumsy to have to type
multiply instead of just *. Also, a case-insensitive match would be much better. For that, you might want to check method lower or how to use regular expressions in Python.Judging from your use of
print, you're aiming at Python 3. If this is the case, then - unlike in Python 2 - you don't need x = float(x)
y = float(y)in function
Divide. Of course, if you want your code to work in both Python 2 & 3, it's O.K. to keep that code. However, even in that case, converting only one of these two variables to float is enough.Like most (all?) interpreted and pseudocompiled languages, Python has
eval function. So, you can basically do result = eval(expression), where expression is a string with any Python code. For example, if you have a string "1+2*3", and send it to eval, you'll get 7 (unless my math is off :-)). This might help you significantly, but be careful to not pass on just any (potentially junky) user's input.One thing seems strange to me. You do
if operation == "Multiply" or operation == "multiply":
12 lines to input x and y
Multiply(x,y)
elif operation == "subtract" or operation == "Subtract":
THE SAME 12 lines to input x and y
Subtract(x,y)
...Why don't you first input
x and y, and then do if-elif block for the actual computation, or, at least, make a function to input x and y and return them (functions in Python can return multiple values)? You have functions for simple expressions which are called only once (multiplication, subtraction, etc), which seem completely unnecessary, but you don't make one for 12 lines of code which are invoked 4 times in your code.Regarding your naming of functions, function names should be lowercase, with words separated by underscores as necessary to improve readability, i.e., you shouldn't capitalize the names of your functions.
Code Snippets
x = float(x)
y = float(y)if operation == "Multiply" or operation == "multiply":
12 lines to input x and y
Multiply(x,y)
elif operation == "subtract" or operation == "Subtract":
THE SAME 12 lines to input x and y
Subtract(x,y)
...Context
StackExchange Code Review Q#31286, answer score: 5
Revisions (0)
No revisions yet.