Python Forum
Could any one help me to evaluate this function. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: Could any one help me to evaluate this function. (/thread-22715.html)



Could any one help me to evaluate this function. - jinghui - Nov-23-2019

Dear all,
I am a student in university and a professor teaching python provided an function example

def calculator(num1=int(input("Type your first number:")),
               operator=input("Enter your +,-,*,/:"),
               num2=int(input("Type our second number:"))):
    calculator={"+":f"{num1+num2}","-":f"{num1-num2}","*":f"{num1*num2}","/":f"{num1/num2}"}
    print(num1,operator,num2,"=",calculator[operator])
calculator()
In my opinion, function should serve single activity. So I think the user interactive function "num1=int...,operator=...,num2=...." should be moved to another function which just deal with user input. I think her codes violates OOP-design-principles like "Single Responsibility Principle". Am I right? I never seen so many things with in a function brackets...


RE: Could any one help me to evaluate this function. - ichabod801 - Nov-23-2019

I'm not a huge fan of the single responsibility principle, but there's another problem with that function. Parameter defaults are evaluated when the function is defined, and then stored with the function. So all of those inputs are done when Python evaluates the function definition. And whatever you enter are the defaults for all time. So if you run the function a second time without providing arguments, it's going to calculate the same numbers. Also, line 4 is really inefficient.


RE: Could any one help me to evaluate this function. - Larz60+ - Nov-23-2019

Here's an expanded view which should help with uunderstanding:
# Expanded:
def calculator():
    num1=int(input("Type your first number: "))
    operator=input("Enter your +,-,*,/: ")
    num2=int(input("Type our second number: "))

    calculator = {
        "+": f"{num1+num2}",
        "-": f"{num1-num2}",
        "*": f"{num1*num2}",
        "/": f"{num1/num2}"
    }
    print(f"{num1}{operator}{num2} = {calculator[operator]}")

calculator()



RE: Could any one help me to evaluate this function. - jinghui - Nov-25-2019

@Larz60+ thanks a lot for helping me do the modification. I will follow the instructions next time. This is my first post~

@ichabod801
Thanks a lot. Now I know what's wrong with her code.

By the way, how many scores will you give to the code I posted(if totally 10 scores)? Is it acceptable in real python project?


RE: Could any one help me to evaluate this function. - ichabod801 - Nov-25-2019

(Nov-25-2019, 03:03 AM)jinghui Wrote: By the way, how many scores will you give to the code I posted(if totally 10 scores)? Is it acceptable in real python project?

It depends on how the assignment was written. If it met the requirements that I put in the assignment, I'd pass it, but barely. However, I doubt I would write an assignment such that the function in the first post would meet the requirements.


RE: Could any one help me to evaluate this function. - jinghui - Nov-26-2019

got it. thanks.