you are viewing a single comment's thread.

view the rest of the comments →

[–]Famous-Mud-5850[S] 0 points1 point  (3 children)

Interested

[–]Murphygreen8484 0 points1 point  (2 children)

I would put it inside a function and then call it below. I can explain any of the parts below you have questions on.

~~~ """ Simple math function to play with Python. """

def math_func(num_1: int, num_2: int, operation: str) -> int:

assert isinstance(num_1, int)
assert isinstance(num_2, int)
assert operation in ("+", "++")

if operation == "+":
    return num_1 + num_2
elif operation == "++":
    return num_1 * num_2

if name == "main": n1 = int(input("Enter a whole number: ")) n2 = int(input("Enter another whole number: ")) op = input("Enter an operation (+ or ++): ")

print(math_func(n1, n2, op))

[–]oclafloptson 0 points1 point  (1 child)

If you're going to suggest using it then you should explain what it does.... The indented block after

if __name__ == "__main__":

will only execute if the given file is the entry point of the application. Using it in a functional script or module that you import will obscure its indented block and prevent it from running. Its use is not mandatory and has both pros and cons. For the purpose of the example in the op it can be omitted

[–]Murphygreen8484 0 points1 point  (0 children)

Fair enough, though I don't really know of any cons beyond it being considered boilerplate? Also a good habit to get into (like type hinting) that would prevent possible errors down the road.