My students want to use input () inside a function. Here is a sample student code, this is a simple calculator:
def calc():
while(True):
req=input(": ")
if req=="":
break
print(req "=" str(eval(req)))
calc()
I always thought that this is not done. Or I'm wrong? Please help me figure it out. And if this is not done, please explain why
PS.we don't talk about return, just about using the input in the body of a function
CodePudding user response:
Yes, you can put input
calls inside a function body. It makes your function impure, which is not ideal from a functional programming point of view, but not every program must be functional.
CodePudding user response:
As the other comments already stated, there is no problem in using input
inside a function.
Note: Other people mention functional programming, but if you're teaching Python and especially input
, I highly doubt this will have any impact on your course as Python is OOP at its core and functional programming may confuse your students.