Skip to main content
Code Review

Return to Revisions

5 of 5
replaced http://stackoverflow.com/ with https://stackoverflow.com/

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."
    
alecxe
  • 17.5k
  • 8
  • 52
  • 93
default

AltStyle によって変換されたページ (->オリジナル) /