-2

When I create a variable and get user input, the program stops until it gets an input from the user. So what I did is I define a function and call it on condition. Now the problem I'm facing is I cannot access the input provided by the user. It says the "VarName" is not defined. I can see it but inside the function.

code example :

def math():
    add = input("Enter value here : ")
math()
print(add)

How do I process the input value? Put it in a variable and use an if statement on a condition using that variable.

Updated question :

I'm having two functions. One gets user input on condition A and the second one gets input on condition B. So only one of them is needed to be shown in the program at a time. This works properly until other place where I want to get the value of the input.

When I try to do return x() and print(y()).. it is calling the function and it is asking for input two times where only one time is needed.

Please tell me how I get the input value without making the program to ask for an inpu.

Den77
  • 19
  • 7
  • Check out [Python input() Function](https://www.w3schools.com/python/ref_func_input.asp). Not clear why you're defining a function just to call the input function. – DarrylG Apr 10 '20 at 00:55
  • there are two possible inputs and getting one at a time is only needed. i'm running this on condition and define input in variable prints and asks for two inputs everytime. – Den77 Apr 10 '20 at 01:29

4 Answers4

0

You can return the variable from inside the function.

def math():
    add = input("Enter value here : ")
    return add

user_input = math()
print(user_input)
Derek O
  • 16,770
  • 4
  • 24
  • 43
  • this line >> user_input = math() is calling the function unnecessarily when I run it. >>print(math()) works good with the input I'm trying to see if that works good in other lines I'm about to write now..thanks.. – Den77 Apr 10 '20 at 01:15
  • Yes, >> user_input = math() is technically unnecessary because you can print the output from the function. However, this way also stores the input as user_input if you need to access it later. Choose whichever method works best for you! – Derek O Apr 10 '20 at 01:26
0

you should make the function return the output what it is supposed to do and you can assign that output to a variable and can use it further

def math():
    add = input("Enter value here : ")
    return add

add = math()
# now you can use add anywhere you want
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • add = math() might help with accessing the input value, but that line of code calls the functions which I previously stopped. I have two functions and they are to be called only on condition. when I add this line to define the input value in a variable it asks for input. to be clear this code works this way. """def math(): add = input("Enter value here : " ) return add add = math()""" add = math() works similiar to math() everytime – Den77 Apr 10 '20 at 01:18
  • @PirateHat well you need to define your problem fully and also tell what you want to achive – sahasrara62 Apr 10 '20 at 01:37
  • `value = func1() if condition else func2()` it will take only one fnctiion value based on result – sahasrara62 Apr 10 '20 at 02:21
  • iput_1= None, input_2 = None if conditon: input_1=func1() else: input_2 = funct2() this way you can take 2 input – sahasrara62 Apr 10 '20 at 02:29
0

Your function needs to return a value. Otherwise when you call it, it won't return anything. If you call your function inside the print statement, it will return the value to the print function to be printed.

def math():
    add = input("Enter value here : ")
    return add
print(math())

Updated:

def math(a, b, opr):
    if opr == "*":
        return a * b
    elif operation == "/":
        return a / b
    elif operation == "+":
        return a + b
    elif operation == "-":
        return a - b
    else:
        print("Invalid operation")
        exit(0)


first_num = float(input("What is your first number? "))
operation = input("what operation (*, /, +, -)? ")
second_num = float(input("What is your second number? "))

print(f"Result: {math(first_num, second_num, operation)}")

Output:

What is your first number? 3
What operation (*, /, +, -)? /
What is your second number? 4
Result: 0.75
Joshua Hall
  • 332
  • 4
  • 15
  • hi, it returns the value to be printed but only after asking me for an input again. is it possible to extract the input value? i have updated my question also kindly check it.. – Den77 Apr 10 '20 at 02:14
  • Yeah I'll check it for you real quick. When I run that exact code, I only asks once because it will only pass line 2 once. – Joshua Hall Apr 10 '20 at 02:19
  • I just updated it. Was that closer to what you were asking? – Joshua Hall Apr 10 '20 at 02:30
  • thank you very much, i never knew i could use a function this efficiently. what will you do when you just try only to grasp the value of "First number" enter for further usage and you are being asked to enter input again. that was my OP too.. :/ – Den77 Apr 10 '20 at 03:06
0

what you can do is

add = None
def math():
    global add
    add = input("Enter value here : ")
math()
print(add)
dxillar
  • 307
  • 2
  • 9