Python 如何通过使用确认的帐户创建功能将用户名和密码写入csv(txt)文件中的记录?

Python 如何通过使用确认的帐户创建功能将用户名和密码写入csv(txt)文件中的记录?,python,python-2.7,csv,Python,Python 2.7,Csv,我想知道如何将“帐户”和“密码3”的用户输入写入csv(txt)文件,作为帐户创建功能的一部分。如果“password_4”没有与“password_3”相同的输入,或者如果任何输入的长度为0,我也会尝试循环该函数。我希望“帐户”和“密码3”都作为一条新记录写入csv(txt)文件中。例如: account_1,password_1 account_2,password_2 etc......,etc....... 这就是我到目前为止的想法: def create_account():

我想知道如何将“帐户”和“密码3”的用户输入写入csv(txt)文件,作为帐户创建功能的一部分。如果“password_4”没有与“password_3”相同的输入,或者如果任何输入的长度为0,我也会尝试循环该函数。我希望“帐户”和“密码3”都作为一条新记录写入csv(txt)文件中。例如:

account_1,password_1
account_2,password_2
etc......,etc.......
这就是我到目前为止的想法:

def create_account():
    account = ""
    print "Create Account"
# Prompts user for input of account, password and password confirmation.
    account = raw_input("Enter login: ")
    password_3 = raw_input("Enter passsword: ")
    password_4 = raw_input("Confirm password: ")
# Sets the condition that if one input has a length of 0, the create_account function will loop.
    while len(account) == 0 or len(password_3) == 0 or len(password_4) == 0:
        create_account()
# Sets the condition that if the password input and password confirmation input do not match, the create_account function will loop.
    while password_3 != password_4:
        create_account()
# Sets the condition that if both of the coniditions set above are false, input will be stored and the loop will be broken.
    else:
        with open('User.csv','w',newline='') as fp:
            a = csv.writer(fp,delimeter=',')
            data = [account,password_3]
            a.writerows(data)
## Writes input to the csv file for later use. Input is stored as one new record.

抱歉,如果这看起来很混乱或复杂。请考虑到我对编码和Python是完全陌生的。谢谢。

您的程序结构应如下所示:

def createAccount():
  account_name = raw_input('Enter login: ')
  password = enterPassword()
  appendAccount('User.csv', account_name, password)

def enterPassword():
  while True: # repeat forever
    password = raw_input('Enter password: ')
    password_again = raw_input('Confirm password: ')
    if len(password) < 5: # or whatever sanity check you might like
      print 'Your password is too short.'
    elif password != password_again:
      print 'Password and confirmation do not match.'
    else:
      return password
      # all is well; 'return' breaks the loop

def appendAccount(file_name, account_name, password):
   with open(file_name, 'a') as csv_file: # Note the 'a' mode for appending
      # use your writing logic, it seems fine
def createAccount():
帐户名称=原始输入('输入登录名:')
密码=输入密码()
appendAccount('User.csv',帐户名,密码)
def enterPassword():
尽管如此:#永远重复
密码=原始输入('输入密码:')
密码\u再次=原始输入('确认密码:')
如果len(password)<5:#或任何您可能喜欢的理智检查
打印“您的密码太短”
elif密码!=再次输入密码。\u:
打印“密码和确认不匹配”
其他:
返回密码
#一切都好“return”打破了循环
def追加帐户(文件名、帐户名、密码):
将open(file_name,'a')作为csv_文件:#注意用于附加的'a'模式
#用你的写作逻辑,看起来不错

尽量避免使用长度超过10行的函数,并尽量考虑如何最恰当地命名它们。一个函数应该做一件明确的事情。我知道,这在开始的几次感觉很傻,但后来你开始意识到程序的可读性有多高。

显然,你对编码一无所知。为什么是3和4(密码中的后缀?…)?我的意思是,为什么不使用密码和重复密码呢?还有,你到底想做什么?那些该死的while循环在干什么?我的意思是,你想让他们做什么,因为现在他们主要是在破坏这段代码……请你更清楚地说明你所写代码的错误。错误(提供完整的回溯)?意外输出(提供输入以及预期和实际输出)?如果您从函数中取出所有的
输入
s并将所需信息作为参数传递,
def create\u account(u\u name,pw,confirm\u pw)
例如,这可能会对您更容易,也会更清楚一点。谢谢您,您帮了大忙!:)