Home > Enterprise >  Can I get function input in python?
Can I get function input in python?

Time:11-15

if I want to know input, how can I do? for example,

def hello(*something):
    return something
hello("a == 1")

then the result will be 'a == 1'. but if I type like this

hello(a == 1)

then the result will be 'True'. I want to make

hello(a == 1) -> a == 1
hello(b == 1) -> b == 1
hello(a == 2) -> a == 2

how can I print something without being string? (I hope just change function) (Also, not just this case, I want to utilize that, so please don't say that why do I have to do like that)

CodePudding user response:

you can't as typing a == 1 is evaluated to True by python first then it is passed to function. even if you type input(a==2) it will gonna throw following error

>>> input(a==2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> 

CodePudding user response:

But why you need to have it not in string format? a == 1 is an operation and to print the way it was written only thing you can do it's put it in quote like this:

 hello("a == 1")

What's the problem to print it in string format?

CodePudding user response:

If you want to return a certain number in your function, just input this number and than cast it to int:


def hello(something):
    a = int(something)

    return a

hello(1)

And if you want to print an integer then just

print(f'a = {hello(1)}')

# a = 1
  • Related