微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么这段代码不会继续在条件循环中停留不止一次迭代?

如何解决为什么这段代码不会继续在条件循环中停留不止一次迭代?

我正在创建一个注册系统,将电子邮件地址和密码保存到文件“users.txt”中。该脚本首先检查文件“users.txt”是否包含给定电子邮件地址的条目,如果找到则返回“使用该电子邮件的帐户已经存在”,然后再次提示用户输入电子邮件地址。该代码在第一次返回错误消息时起作用,但如果您再次输入相同的电子邮件,它不会返回与预期相同的错误消息,而是继续执行其余代码。在给出不在文件中的电子邮件地址之前,我如何保持循环?到目前为止,这是我的代码

#!/usr/bin/env python3

import getpass

class NewUser:
    def __init__(self):
        self.username = input('Enter your email address: ')
        checkUser = NewUser.userExists(self.username)
        while True:
            if checkUser == False:
                self.username = input('Enter your email address: ')
            if checkUser == True:
                break
        self.password = getpass.getpass(prompt='Choose a Password: ',stream=None)
        self.confirm = getpass.getpass(prompt='Confirm Password: ',stream=None)
        while True:
            if self.password != self.confirm:
                print('Passwords do not match,try again.')
                self.password = getpass.getpass(prompt='Choose a Password: ',stream=None)
                self.confirm = getpass.getpass(prompt='Confirm Password: ',stream=None)
            if self.password == self.confirm:
                break
        print('Account has been successfully created')
        with open('users.txt','a+') as f:
            f.write('\n')
            f.write(self.username)
            f.write(',')
            f.write(self.password)
            f.close()

    def userExists(username):
        with open('users.txt','rb') as f:
            if bytes(username,encoding='utf-8') in f.read():
                print('An account with that email already exists.')
                return(False)
            else:
                return(True)

解决方法

您构建代码的方式存在很多问题,但我们将只关注您提到的问题。我只是打算稍微重写一下代码,以展示希望有帮助的解决方案。

class NewUser:
    def __init__(self):
       user_exist = True
       while user_exist:
          self.username = input('Enter your email address: ')
          user_exist = NewUser.userExists(self.username)
          if user_exist:
             print('An account with that email already exists.')
          # here prompt for asking email will keep coming till entered email 
          # is not in given file
    
       pass_match = False
       while not pass_match:
            self.password = getpass.getpass(prompt='Choose a Password: ',stream=None)
            self.confirm = getpass.getpass(prompt='Confirm Password: ',stream=None)
            pass_match = self.password == self.confirm
            if not pass_match:
               print('Passwords do not match,try again.')

       print('Account has been successfully created')
       with open('users.txt','a+') as f:
          f.write(self.username+','+ self.password+'\n')
          f.close()

   def userExists(username):
       with open('users.txt','rb') as f:
           return bytes(username,encoding='utf-8') in f.read()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。