0
Can anyone help me find the mistake ,I’m having hard time finding mistake in my code
#simple python console bot x = input ("say hi") def gender(): gend = input("your gender?(male or female)") def user_name(): user = input("enter your name ?") print("hello", user) return ; if x == "hi": user_name() gender() if gend == "male": print ("hello mr.",user) elif gend == "female": print ("hello mrs.",user) elif x == "bye": print ("bye")
1 Resposta
+ 2
"gend" is a local variable in the function gender() and cannot be used/accessed outside of the function. That's why you'll get a NameError if you try to run the code.
You could declare a global variable and change it in the function like this:
gend = '' # declare global variable
def gender():
global gend # use global variable within the function
gend = input("your gender?(male or female)")
Same with "user" and the function user_name().