你好,我对 python 编程很陌生。我正在开始我的第一个程序,但我遇到了一些麻烦..有没有更好的方法来执行下面的代码片段?当我运行该程序时,我得到“yes_no”未定义。
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
定义于main()
;代码在wrong()
看不到它(范围错误)。
另外,你还有main()
call wrong()
哪个调用main()
无限循环中。
你可能想要更多类似的东西
def is_yes(s):
return s.strip().lower() in ('y', 'yes')
def is_no(s):
return s.strip().lower() in ('n', 'no')
def existing_user():
# ok, they are signed in; now what?
pass
def sign_up():
# what can I sell you today?
pass
def main():
print('Welcome to registration.')
while True:
inp = raw_input('Are you an authorized user? ')
if is_yes(inp):
return existing_user()
elif is_no(inp):
return sign_up()
if __name__=="__main__":
main()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)