Python Forum
Could any one help me to evaluate this function.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Could any one help me to evaluate this function.
#1
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...
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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()
Reply
#4
@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?
Reply
#5
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
got it. thanks.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020