一个缩进问题
这个是python核心编程里的例子,本身有缩进问题我改了之后又有新问题
db = {}
def newuser():
prompt = 'login desired:'
while True:
name = raw_input(prompt)
if db.has_key(name):
prompt = 'name taken,try again:'
continue
else:
break
pwd = raw_input('passwd:')
db[name] = pwd
def olduser():
name = raw_input('login:')
pwd = raw_input('passwd:')
passwd = db.get(name)
if passwd == pwd:
print 'welcome back',name
else:
print 'login incorrect'
def showmenu():
prompt = '''
(N)ew User Login
(E)xiting User Login
(Q)iut
Enter choice: '''
done = False
while not done:
chosen = False
while not chosen:
try:
choice = raw_input(prompt).strip()[0].lower()
except (EOFError,KeyboardInterrupt):
choice = 'q'
print '\nYou picked:[%s]' % choice
if choice not in 'neq':
print 'invalid option,try again'
else:
chosen = True
if choice == 'q':done = True
if choice == 'n':newuser()
if choice == 'e':olduser()
if __name__ == '__main__':
showmenu()
问题出现在这几行
prompt = '''
(N)ew User Login
(E)xiting User Login
(Q)iut
Enter choice: '''
done = False
while not done:
chosen = False
while not chosen:
try:
choice = raw_input(prompt).strip()[0].lower()
错误时prompt没有定义。但是我单独运行
prompt = '''
(N)ew User Login
(E)xiting User Login
(Q)iut
Enter choice: '''加上choice = raw_input(prompt).strip()[0].lower()却能够显示,求大神帮助
[解决办法]
done及后面一段应该也要缩进的,是def里面的内容