我的登录方法没有在我的 register() 中调用,我在 mainError(0 中的错误在不应该出现时出现



这是我的代码:

import time
print("Welcome to the quiz")
print("Would you like to login with an existing account or register for a new account?")
def login():
userQ = input("Please enter your username: ")
passQ = input("Please enter your password: ")
#In progress of making...
def register():
print ("Your username will now be created from your first name and age.")
fname = input("Please enter your first name: ")
age = input("Please enter your age: ")
fname2 = fname[:3]
username = fname2 + age
print ("Your username is {}".format(username.upper()))
password = input("Please enter a password for your new account: ")
time.sleep(1.0)
print ("Username: {}".format(username))
print ("Password: {}".format(password))
with open("UserPass.csv", "w") as f:
for line in f:
file = line.split(",")
f.write(username) in file[0]
print ("ACCOUNT CREATED")
login()
class validation(Exception):
def __init__(self, error):
self.error = error
def printError(self):
print ("Error: {} ".format(self.error))
def mainError():
try:
raise validation('Please enter a valid input')
except validation as e:
e.printError()
def Menu():
while True:
options = ["Login", "Register"]
print("Please, choose one of the following options")
num_of_options = len(options)
for i in range(num_of_options):
print("press " + str(i + 1) + " to " + options[i])
try:
uchoice = int(input("? "))
if uchoice == 1:
print("You chose to " + options[uchoice - 1])
login()
break
elif uchoice == 2:
print("You chose to " + options[uchoice - 1])
register()
break
elif (uchoice - 1) > len(options):
mainError()
except ValueError:
mainError()
Menu()

在我的代码中,如果用户选择注册一个新帐户,它会运行register()方法。用户在方法中输入用户名和密码后,它将其写入csv文件,然后调用login()方法。但是,相反。。。它以某种方式运行Menu()方法,并忽略我的login()。

此外,在到达register方法的末尾后,它会打印一条错误消息,这是我用"Class Validation(Exception)"one_answers"mainError()"生成的,我不希望它这样做——我不明白它为什么这么做,因为我没有调用mainError(。

当我运行代码并选择注册时的一个示例:

Welcome to the quiz
Would you like to login with an existing account or register for a new account?
Please, choose one of the following options
press 1 to Login
press 2 to Register
? 2
You chose to Register
Your username will now be created from your first name and age.
Please enter your first name: Siddharth
Please enter your age: 16
Your username is SID16
Please enter a password for your new account: shamrock
Username: Sid16
Password: shamrock
Error: Please enter a valid input 
Please, choose one of the following options
press 1 to Login
press 2 to Register
? 

正如你所看到的,它在注册后和注册前都会转到Manu()而不是login(),在不需要的时候会打印"错误:请输入有效的输入"。

我该怎么解决这个问题。非常感谢。

更改

with open("UserPass.csv", "w") as f:
for line in f:
file = line.split(",")
f.write(username) in file[0]

with open('UserPass.csv', 'a') as f:
f.write('whatever text you want to save')

相关内容

最新更新