HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Calculator that does basic functions and area/volume in Python 3.6

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thatpythondoesandfunctionsvolumecalculatorareabasic

Problem

This is my first big project that I've made in Python. I've noticed as I was writing it that it's extremely repetitive and I just don't know how to make it less so.

```
# This is a basic calculator that also has area/volume calculations for certain shapes.

import math
import sys

# Main menu
def main():
choice = input('Which operation would you like?\n'
'1) Area\n'
'2) Volume\n'
'3) Basic Math\n'
'4) Quit\n')

if choice == '1':
area()
elif choice == '2':
volume()
elif choice == '3':
basic_math()
elif choice == '4':
sys.exit('Choice 4 selected. Quitting...')
else:
print('I didn\'t understand your input!')

# Menu to choose which area you'd like to calculate
def area():
choice = input('Please enter what you\'d like to calculate\n'
'1) Area of a square\n'
'2) Area of a rectangle\n'
'3) Area of a circle\n'
'4) Area of a triangle\n'
'5) Area of a equilateral triangle\n'
'6) Area of a trapezoid\n'
'7) Area of a cube\n'
'8) Quit\n'
'9) Main Menu\n')

try:
if choice == '1':
area_of_square()

elif choice == '2':
area_of_rectangle()

elif choice == '3':
area_of_circle()

elif choice == '4':
area_of_triangle()

elif choice == '5':
area_of_equilateral_triangle()

elif choice == '6':
area_of_trapezoid()

elif choice == '7':
area_of_cube()

elif choice == '8':
sys.exit('Choice 8 selected. Quitting...')

elif choice == '9':
main()

else:
print('I didn\'t understand your input!')
main()
except NameError:
print('You forgot to define a function!'

Solution

The main problem is the repetetiveness and nestedness when it comes to reading user inputs and mapping them to the function calls. You are currently using multiple if/else branches, but, what if you would use a dictionary to map choices into function names:

COMMANDS = {
    '1': addition,
    '2': subtraction,
    '3': multiplication,
    '4': division,
    '5': exit,
    '6': main
}

if choice not in COMMANDS:
    print('I\'m sorry, I didn\'t understand your input.')
    basic_math()
else:
    COMMANDS[choice]()


Note that exit here is a function you might have to exit the app.

Also, look through the third-party apps in the CLI space - there might be a tool that can ease creating this kind of question-choice style programs.

Here are some other notes:

  • the program is too long - split it into multiple logical parts to have a better separation of concern and modularity. For example, the calculations of areas and volumes should be separated from the question-choice handling functions



  • you can use multi-line strings instead of having regular strings with newline characters



-
you can wrap the string inside the print() statement around double quotes so that you won't need to escape the single quote (credit to @Dex'ter):

"I'm sorry, I didn't understand your input."

Code Snippets

COMMANDS = {
    '1': addition,
    '2': subtraction,
    '3': multiplication,
    '4': division,
    '5': exit,
    '6': main
}

if choice not in COMMANDS:
    print('I\'m sorry, I didn\'t understand your input.')
    basic_math()
else:
    COMMANDS[choice]()
"I'm sorry, I didn't understand your input."

Context

StackExchange Code Review Q#155241, answer score: 4

Revisions (0)

No revisions yet.