gpt4 book ai didi

python - 如何修复写入文件后 3 行重新启动的函数?

转载 作者:行者123 更新时间:2023-12-01 07:23:03 25 4
gpt4 key购买 nike

为了我自己的教育值(value),我正在使用 csv 库创建一个简单的登录脚本。当我使用函数 register(); 将登录详细信息附加到文件后,我的代码似乎在第 63 行重新启动:

password = input('Please enter your chosen password: ')

我尝试找到类似的问题(没有成功),我猜测这与我的结构有关。

# login.py
""" Logs in reading from a CSV
text file
"""

# Imports
import csv

# Functions
def reset_login_file():
""" Resets the users.txt
file
"""

with open('users.txt', 'w', newline='') as csvfile:
csv_writer = csv.writer(csvfile)

csv_writer.writerow(['Admin', 'password'])

def login():
""" Checks users.txt and
logs in
"""

print('')

def check_for_name(name_chosen):
""" Checks for name_chosen in
users.txt
"""

with open('users.txt', 'r', newline='') as check_csvfile:
csv_reader = csv.reader(check_csvfile)

for row in csv_reader:
if row[0] == name_chosen:
print('Name is taken, please try again!')
register()
break

def password_checker(password_chosen):
""" Checks the password
meets criteria
"""

stop_loop = False
while not stop_loop:
if len(password_chosen) > 6 and len(password_chosen) < 13:
print('Valid Password!')
stop_loop = True
else:
print('Invalid Password, please try again!')
register()
break

def register():
""" Registers the user in
users.txt
"""

username = input('Please enter your chosen username: ')
check_for_name(username)
password = input('Please enter your chosen password: ')
password1 = input('Please re-enter your chosen password: ')
if password == password1:
password_checker(password)
else:
print('Please try again!')
register()

print('Creating your account!')
with open('users.txt', 'a', newline='') as csvfile:
csv_writer = csv.writer(csvfile)

csv_writer.writerow([username, password])

# Code here - Interactive loop
stop = False
while not stop:
print('Welcome!\nTo view commands, please press Enter!')
input()
print("""COMMANDS
Reset Login File [RLF]
Register [R]
Login [L]
Quit [Q]
""")
choice = input('Select a Command: ')

if choice == 'r':
register()
elif choice == 'rlf':
reset_login_file()
elif choice == 'l':
login()
elif choice == 'q':
stop = True
else:
print('Choose a valid command')
<小时/>

来自 shell 的文本:

Welcome!  
To view commands, please press Enter!

COMMANDS
Reset Login File [RLF]
Register [R]
Login [L]
Quit [Q]

Select a Command: r
Please enter your chosen username: fred
Name is taken, please try again!
Please enter your chosen username: red
Please enter your chosen password: bobsred
Please re-enter your chosen password: bobsred
Valid Password!
Creating your account!
Please enter your chosen password: redhead
Please re-enter your chosen password: redhead
Valid Password!
Creating your account!
Welcome!
To view commands, please press Enter!
<小时/>

users.txt 内容为:

Admin,password  
bob,password
fred,password
red,bobsred
fred,redhead

我希望这会注册用户,然后返回到交互循环。

最佳答案

您会看到对 register() 的递归调用展开并从中断处继续。

有帮助的一件事是,将辅助函数设计为在一切正常时返回 True,如果用户需要重试,则返回 False。然后使用 register() 内的循环根据需要多次调用它们。

这是一个例子:

# login.py
""" Logs in reading from a CSV
text file
"""

# Imports
import csv

# Functions
def reset_login_file():
""" Resets the users.txt
file
"""

with open('users.txt', 'w', newline='') as csvfile:
csv_writer = csv.writer(csvfile)

csv_writer.writerow(['Admin', 'password'])

def login():
""" Checks users.txt and
logs in
"""

print('')

def check_for_name(name_chosen):
""" Checks for name_chosen in
users.txt
"""

with open('users.txt', 'r', newline='') as check_csvfile:
csv_reader = csv.reader(check_csvfile)

for row in csv_reader:
if row[0] == name_chosen:
print('Name is taken, please try again!')
return False
return True

def password_checker(password_chosen):
""" Checks the password
meets criteria
"""

if len(password_chosen) > 6 and len(password_chosen) < 13:
print('Valid Password!')
return True
else:
print('Invalid Password, please try again!')
return False

def register():
""" Registers the user in
users.txt
"""

while True:
username = input('Please enter your chosen username: ')
if check_for_name(username):
break
while True:
password = input('Please enter your chosen password: ')
password1 = input('Please re-enter your chosen password: ')
if password == password1:
password_checker(password)
break
else:
print('Please try again!')

print('Creating your account!')
with open('users.txt', 'a', newline='') as csvfile:
csv_writer = csv.writer(csvfile)

csv_writer.writerow([username, password])

# Code here - Interactive loop
stop = False
while not stop:
print('Welcome!\nTo view commands, please press Enter!')
input()
print("""COMMANDS
Reset Login File [RLF]
Register [R]
Login [L]
Quit [Q]
""")
choice = input('Select a Command: ')

if choice == 'r':
register()
elif choice == 'rlf':
reset_login_file()
elif choice == 'l':
login()
elif choice == 'q':
stop = True
else:
print('Choose a valid command')

关于python - 如何修复写入文件后 3 行重新启动的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57592527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com