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

prabodhprakash
  • 3,825
  • 24
  • 48
Michael Vu
  • 13
  • 3

3 Answers3

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) 

Yi Hang Tay
  • 166
  • 8
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.

shuberman
  • 1,416
  • 6
  • 21
  • 38
-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)    
Gilseung Ahn
  • 2,598
  • 1
  • 4
  • 11