python - 我的登录方法未在我的register()中调用,并且在mainError(0)中出现我的错误(如果不应该这样做)

标签 python python-3.x loops error-handling

这是我的代码:

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)和'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')

关于python - 我的登录方法未在我的register()中调用,并且在mainError(0)中出现我的错误(如果不应该这样做),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46634908/

相关文章:

Python 将列表拆分为开始和结束关键字模式的子列表

python - C++ std::vector 乘法中是否存在已知的不一致行为?

linux - 定义范围的 bash for 循环不起作用

python - 如何使用递归找到系列 1+ (1*2) + (1*2*3) … (1*2*3*…n) 的总和?

python-3.x - 如何在终端中删除给定的args并在argparse中添加自己的args

c++ - 数组中的重复数字

c - C 中的函数和循环问题

python - Linux Python 2.7 odbc 连接到 Progress Open Edge DB

python - python if/else 中的列表理解

python - 如何在 Nexmo 中检索可用的电话号码?