鱼C论坛

 找回密码
 立即注册
查看: 1310|回复: 2

有关26课字典的动动手

[复制链接]
发表于 2019-8-4 22:58:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这是一个登录程序:

user_data = {}

def new_user():
    prompt = '请输入用户名:'
    while True:
        name = input(prompt)
        if name in user_data:
            prompt = '此用户名已经被使用,请重新输入:'
            continue
        else:
            break

    passwd = input('请输入密码:')
    user_data[name] = passwd
    print('注册成功,赶紧试试登录吧^_^')

def old_user():
    prompt = '请输入用户名:'
    while True:
        name = input(prompt)
        if name not in user_data:
            prompt = '您输入的用户名不存在,请重新输入:'
            continue
        else:
            break
   
    passwd = input('请输入密码:')
    pwd = user_data.get(name)
    if passwd == pwd:
        print('欢迎进入XXOO系统,请点右上角的X结束程序!')
    else:
        print('密码错误!')

def showmenu():
    prompt = '''
|--- 新建用户:N/n ---|
|--- 登录账号:E/e ---|
|--- 推出程序:Q/q ---|
|--- 请输入指令代码:'''

    while True:
        chosen = False
        while not chosen:
            choice = input(prompt)
            if choice not in 'NnEeQq':
                print('您输入的指令代码错误,请重新输入:')
            else:
                chosen = True

        if choice == 'q' or choice == 'Q':
            break
        if choice == 'n' or choice == 'N':
            new_user()
        if choice == 'e' or choice == 'E':
            old_user()

showmenu()

我不懂    while True:
        chosen = False
        while not chosen:
            choice = input(prompt)
            if choice not in 'NnEeQq':
                print('您输入的指令代码错误,请重新输入:')
            else:
                chosen = True
这一部分是什么意思,谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-8-4 23:11:23 | 显示全部楼层
这部分就是判断输入是否异常的啊,嵌套的while循环默认是True,如果输入指令不是NnEeQq那么就一直循环;如果在这个范围循环结束(chosen = True, while的判断就变为False了是不是),继续到下面三个判断是要退出还是新建还是登录用户。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-5 10:05:11 | 显示全部楼层
    while True:                 #死循环
        chosen = False          #设置一个布尔类型的变量 为了结束下面的while not chosen:循环
        while not chosen:       #第一次执行 not chosen(此时chosen为假) 为真
            choice = input(prompt)      #choice为输入值
            if choice not in 'NnEeQq':      #如果输入的值不为'NnEeQq'这几个字母 提示重新输入
                print('您输入的指令代码错误,请重新输入:')
            else:
                chosen = True               #否则chosen为真  while not chosen: 这个循环跳出
   
        #下面就是根据你输入的值 对应执行就OK了
        if choice == 'q' or choice == 'Q':
            break
        if choice == 'n' or choice == 'N':
            new_user()
        if choice == 'e' or choice == 'E':
            old_user()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-17 11:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表