Hi I'm very new with programming in python. I'm starting my first program but i'm having a little trouble.. is there a better way of doing the snippet of code below? When i run the program I get "yes_no" not defined.
def main():
print "\n Welcome to registration. Are you an authorized user?\n"
yes_no = raw_input("Yes or No?: ")
if yes_no in ("Yes", "yes"):
return oldUser()
elif yes_no in ("y", "Y"):
print ("Please enter YES")
return wrong()
elif yes_no in ("n", "N"):
return wrong()
else:
print ("\n Unauthorized text!\n")
return main()
def wrong():
if yes_no in ("y", "Y"):
print ("Please Enter Yes!")
return main()
else:
yes_no in ("n", "N")
print ("Please Enter No!")
return main()
yes_no is not defined in wrong() method, so it is only visible in main(). You can pass yes_not into wrong() like wrong(yes_no)
You defined a variable called
yes_no
in the functionmain
, and that's the only place it exists.Now, you could pass it into
wrong
as an argument:but this ignores the problem that your main/wrong functions are mutually recursive, which in this case is a poor fit for what you're trying to do.
It'd be nicer here to unroll the recursion into a loop:
NB that you may ask the user to enter "No", but never handle that as valid input.
yes_no
is defined inmain()
; code inwrong()
cannot see it (wrong scope).Also, you have
main()
callwrong()
which callsmain()
in an infinite loop.You probably want something more like