0
def xdgt(x): if x is "m": a = True print(a) else: a = False print(a) x = input("Are you Male or Female? please only input m or f:") xdgt(x) print(a) 

Result:

Traceback (most recent call last): File "/tmp/sessions/dd8fb527f68c80d1/main.py", line 10, in print(a) NameError: name 'a' is not defined

2
  • Its hard to comment without knowing how you have formatted your python code. Commented May 19, 2020 at 5:12
  • Don’t use is to compare strings, use ==. In short, your function should be written as return x == 'm'. Commented May 19, 2020 at 5:36

3 Answers 3

2

Add a return a to your function

def xdgt(x): if x is "m": a = True else: a = False return a 
a = xdgt(x) print(a) 
Sign up to request clarification or add additional context in comments.

1 Comment

Precisely to the point, thank you
0

It seems like you want the function to return the value. In which case you can change your code as follows:

def xdgt(x): if x is "m": a = True else: a = False return a x = input("Are you Male or Female? please only input m or f:") return_val= xdgt(x) print(return_val) 

Takeaway here:

Instead of printing the value inside the function you should return it and store it in a variable so that you can use it to perform functions you desire like printing the value in this case.

1 Comment

Yes this is exactly what I need. Thank you
-2

If you want to use local variable a outside of the function, use global.

def xdgt(x): global a if x == 'm': a = True else: a = False x = input("Are you Male or Female? please only input m or f:") xdgt(x) print(a) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.