鱼C论坛

 找回密码
 立即注册
查看: 456|回复: 3

[已解决]Python第26讲动动手

[复制链接]
发表于 2020-4-2 10:47:37 | 显示全部楼层 |阅读模式

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

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

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()
我想在源代码的基础上能够再添加两个功能。(1)代码输入错误时可以直接在后面重新输入,|--- 新建用户:N/n ---|
|--- 登录账号:E/e ---|
|--- 推出程序:Q/q ---|
|--- 请输入指令代码:'''而不是又打出这一段。(2)old_user密码输入错误之后可以有重新输入密码的机会
求问各位大佬如何添加?
2、求问各位大佬,为什么每次循环都会打出 '''
|--- 新建用户:N/n ---|
|--- 登录账号:E/e ---|
|--- 推出程序:Q/q ---|
|--- 请输入指令代码:''',如果上次输入的指令为e/n,下次循环的时候choice的值不应该保存了n/e吗?为什么还是 '''
|--- 新建用户:N/n ---|
|--- 登录账号:E/e ---|
|--- 推出程序:Q/q ---|
|--- 请输入指令代码:'''???


最佳答案
2020-4-2 11:39:02
1.你看下是这样吗

  1. user_data = {}

  2. def new_user():
  3.     prompt = '请输入用户名:'
  4.     while True:
  5.         name = input(prompt)
  6.         if name in user_data:
  7.             prompt = '此用户名已经被使用,请重新输入:'
  8.             continue
  9.         else:
  10.             break

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

  14. def old_user():
  15.     prompt = '请输入用户名:'
  16.     while True:
  17.         name = input(prompt)
  18.         if name not in user_data:
  19.             prompt = '您输入的用户名不存在,请重新输入:'
  20.             continue
  21.         else:
  22.             break
  23.    
  24.     passwd = input('请输入密码:')
  25.     pwd = user_data.get(name)
  26.     vol = 2
  27.     while vol:
  28.         vol -= 1
  29.         if passwd == pwd:
  30.             print('欢迎进入XXOO系统,请点右上角的X结束程序!')
  31.         else:
  32.             if vol:
  33.                 print('密码错误,请重新输入!')
  34.                 passwd = input('请输入密码:')
  35.             else:
  36.                 print('密码错误!')

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

  43.     while True:
  44.         chosen = False
  45.         while not chosen:
  46.             index = 2
  47.             choice = input(prompt)
  48.             while index:
  49.                 if index:
  50.                     if choice not in 'NnEeQq' and index == 2:
  51.                         choice = input('您输入的指令代码错误,请重新输入:')
  52.                     elif choice not in 'NnEeQq' and index == 1:
  53.                         print('您输入的指令代码错误,请重新输入!')
  54.                     else:
  55.                         chosen = True
  56.                         break
  57.                 else:
  58.                     break
  59.                 index -= 1

  60.         if choice == 'q' or choice == 'Q':
  61.             break
  62.         if choice == 'n' or choice == 'N':
  63.             new_user()
  64.         if choice == 'e' or choice == 'E':
  65.             old_user()

  66. showmenu()
复制代码

2.因为它每次循环都有choice = input(prompt),不知道是不是这个意思
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-2 11:02:12 | 显示全部楼层
可不可以描述的详细一点?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-2 11:39:02 | 显示全部楼层    本楼为最佳答案   
1.你看下是这样吗

  1. user_data = {}

  2. def new_user():
  3.     prompt = '请输入用户名:'
  4.     while True:
  5.         name = input(prompt)
  6.         if name in user_data:
  7.             prompt = '此用户名已经被使用,请重新输入:'
  8.             continue
  9.         else:
  10.             break

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

  14. def old_user():
  15.     prompt = '请输入用户名:'
  16.     while True:
  17.         name = input(prompt)
  18.         if name not in user_data:
  19.             prompt = '您输入的用户名不存在,请重新输入:'
  20.             continue
  21.         else:
  22.             break
  23.    
  24.     passwd = input('请输入密码:')
  25.     pwd = user_data.get(name)
  26.     vol = 2
  27.     while vol:
  28.         vol -= 1
  29.         if passwd == pwd:
  30.             print('欢迎进入XXOO系统,请点右上角的X结束程序!')
  31.         else:
  32.             if vol:
  33.                 print('密码错误,请重新输入!')
  34.                 passwd = input('请输入密码:')
  35.             else:
  36.                 print('密码错误!')

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

  43.     while True:
  44.         chosen = False
  45.         while not chosen:
  46.             index = 2
  47.             choice = input(prompt)
  48.             while index:
  49.                 if index:
  50.                     if choice not in 'NnEeQq' and index == 2:
  51.                         choice = input('您输入的指令代码错误,请重新输入:')
  52.                     elif choice not in 'NnEeQq' and index == 1:
  53.                         print('您输入的指令代码错误,请重新输入!')
  54.                     else:
  55.                         chosen = True
  56.                         break
  57.                 else:
  58.                     break
  59.                 index -= 1

  60.         if choice == 'q' or choice == 'Q':
  61.             break
  62.         if choice == 'n' or choice == 'N':
  63.             new_user()
  64.         if choice == 'e' or choice == 'E':
  65.             old_user()

  66. showmenu()
复制代码

2.因为它每次循环都有choice = input(prompt),不知道是不是这个意思
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-2 16:12:57 | 显示全部楼层
kylin121380 发表于 2020-4-2 11:39
1.你看下是这样吗

2.因为它每次循环都有choice = input(prompt),不知道是不是这个意思

感谢大神!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 15:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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