huyanmin 发表于 2021-1-15 13:08:46

Python 26课习题 continue是否添加问题,求详解


user_data = {}

def new_user():
    prompt = '请输入用户名:'
    while True:
      name = input(prompt)
      if name in user_data:
            prompt = '此用户名已经被使用,请重新输入:'
            continue      #此处加continue语句好像没有必要啊,因为执行了if语句下面的else语句就不再执行了,就又返回到while循环
      else:
            break

    passwd = input('请输入密码:')
    user_data = 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()

昨非 发表于 2021-1-15 13:17:38

可有可无的吧
因为continue是跳出本次循环,而执行到这一步,此次循环已经结束了
测试一下不影响运行结果就可以去掉
页: [1]
查看完整版本: Python 26课习题 continue是否添加问题,求详解