Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet103/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   99   100   101   102   103   104   105   106   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Handling User Registration


Next, we’ll design the function for registering users. Let’s check out that functionality:

5| # handle user registration and writing to csv 6| def registerUser( ):
7| with open("users.csv", mode="a", newline="") as f:
8| writer = csv.writer(f, delimiter=",")
10| print("To register, please enter your info:")
11| email = input("E-mail: ")
12| password = input("Password: ")
13| password2 = input("Re-type password: ")
15| clear_output( ) 17| if password == password2:
18| writer.writerow( [email, password] )
19| print("You are now registered!") 20| else:
21| print("Something went wrong. Try again.")

We start by defining the function and opening a CSV file called “user.csv”. This will be the file where we store our data. We create a writer object with that file that will allow us to append additional data. After asking the user for their information, we check that both passwords entered are the same, and either add the user with the writer object we created, or we let the user know that something went wrong. Feel free to call this function and try it out. You should see the file be created after the first attempt.

Handling User Login


The second task that we need to design is the ability to log users in. Let’s see how to do that:

23| # ask for user info and return true to login or false if incorrect info 24| def loginUser( ):
25| print("To login, please enter your info:")
26| email = input("E-mail: ")
27| password = input("Password: ")
29| clear_output( ) 31| with open("users.csv", mode="r") as f:
32| reader = csv.reader(f, delimiter=",") 34| for row in reader:
35| if row == [email, password]:
36| print("You are now logged in!")
37| return True
39| print("Something went wrong, try again.")
40| return False

In the user login function, we ask the user to enter their information. We then open the file where the user information is being stored as read-only mode. A reader object is created using the CSV library, and we loop through the data row by row on line 34. Each row we read is in the form of a list with two items. The first item is always the e-mail, and the second is the password. On line 35 we’re checking the row information against a temporary list filled with the information that the user inputs. If the data matches, we log them in and return True; otherwise, we tell them something went wrong and return False. Try calling this function after registering.
Note The file is stored in the same directory as the notebook file.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   99   100   101   102   103   104   105   106   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish