Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
  • There's no need for parentheses around if/elif/else statements. They can be written like this:

     if condition:
     ...
    
  • You should be using an if __name__ == "__main__" if __name__ == "__main__" guard. This ensures that if your code is imported to another file, certain code will not be run. This means that this function call right here:

     main()
    

    Should become this:

     if __name__ == "__main__":
     main()
    
  • There's no need to import math twice. Once it's imported, it's functions and variables already populate the file's namespace.

  • Finally, if you're going to write comments for your functions (in this case your comments are fairly useless), you should be using docstrings instead. A typical docstring looks something like this:

     def my_func( ... ):
     """Brief description.
     More detailed description.
     Keyword arguments:
     argument -- Argument description.
     """
     ...
    

    Keep in mind though, if your code is self-commenting, and clearly written, the comments probably aren't needed.

  • There's no need for parentheses around if/elif/else statements. They can be written like this:

     if condition:
     ...
    
  • You should be using an if __name__ == "__main__" guard. This ensures that if your code is imported to another file, certain code will not be run. This means that this function call right here:

     main()
    

    Should become this:

     if __name__ == "__main__":
     main()
    
  • There's no need to import math twice. Once it's imported, it's functions and variables already populate the file's namespace.

  • Finally, if you're going to write comments for your functions (in this case your comments are fairly useless), you should be using docstrings instead. A typical docstring looks something like this:

     def my_func( ... ):
     """Brief description.
     More detailed description.
     Keyword arguments:
     argument -- Argument description.
     """
     ...
    

    Keep in mind though, if your code is self-commenting, and clearly written, the comments probably aren't needed.

  • There's no need for parentheses around if/elif/else statements. They can be written like this:

     if condition:
     ...
    
  • You should be using an if __name__ == "__main__" guard. This ensures that if your code is imported to another file, certain code will not be run. This means that this function call right here:

     main()
    

    Should become this:

     if __name__ == "__main__":
     main()
    
  • There's no need to import math twice. Once it's imported, it's functions and variables already populate the file's namespace.

  • Finally, if you're going to write comments for your functions (in this case your comments are fairly useless), you should be using docstrings instead. A typical docstring looks something like this:

     def my_func( ... ):
     """Brief description.
     More detailed description.
     Keyword arguments:
     argument -- Argument description.
     """
     ...
    

    Keep in mind though, if your code is self-commenting, and clearly written, the comments probably aren't needed.

Source Link
Ethan Bierlein
  • 15.9k
  • 4
  • 60
  • 146

Input validation

You do absolutely no input validation when the user enters an integer. If the user enters a non-integer value, like foo, or bar, your code will crash with a ValueError. The correct way to obtain user input is to use a try/except block, like this:

try:
 var1 = int(input( ... ))
 var2 = int(input( ... ))
 ...
except ValueError:
 print("Invalid input!")
 ... 

By doing this, you can be sure that your program won't crash on invalid input.


Input mappings

Right now, you're chaining if/elif/else statements to check user input. This is not an optimal way to do this. Rather, you can create a dictionary mapping, like the below, for possible inputs:

OPERATIONS = {
 "+": add,
 "-": sub,
 ...
}

Then, all you have to do to get, and run user input is something like this:

operation = input( ... )
if operation in OPERATIONS:
 OPERATIONS[operation](var1, var2)
else:
 print("Invalid operation!")

This makes your code much clearer, and easier to read, rather than a chain of if/elif/else statements.


Use math and operator

There is absolutely no reason to define your own mathematical functions, just so you can use them in a dictionary mapping, you should be using the math and operator modules instead. These all provide built-in functions, like add, and sub, or values, like \$\pi\$ or \$e\$.


General nitpicks

  • There's no need for parentheses around if/elif/else statements. They can be written like this:

     if condition:
     ...
    
  • You should be using an if __name__ == "__main__" guard. This ensures that if your code is imported to another file, certain code will not be run. This means that this function call right here:

     main()
    

    Should become this:

     if __name__ == "__main__":
     main()
    
  • There's no need to import math twice. Once it's imported, it's functions and variables already populate the file's namespace.

  • Finally, if you're going to write comments for your functions (in this case your comments are fairly useless), you should be using docstrings instead. A typical docstring looks something like this:

     def my_func( ... ):
     """Brief description.
     More detailed description.
     Keyword arguments:
     argument -- Argument description.
     """
     ...
    

    Keep in mind though, if your code is self-commenting, and clearly written, the comments probably aren't needed.

lang-py

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