[英]my login method is not being called within my register() and my error in mainError(0 comes up when it's not supposed to
這是我的代碼:
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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.