How to verify username and password from CSV file in Python?

2024/11/18 10:21:05

I am doing a Python project where I have to verify my username and password from a csv file where the first two rows and columns have the username and password as 'hi'.

Current Code:

    answer = input("Do you have an account?(yes or no) ")
if answer == 'yes' :login = Falsecsvfile = open("Username password.csv","r")reader = csv.reader('Username password.csv')username = input("Player One Username: ")password = input("Player One Password: ")for row in reader:if row[0]== username and row[1] == password:login = Trueelse:login = Falseif login == False:print("Incorrect. Game Over.")exit()else:print("You are now logged in!")
else:print('Only Valid Usernames can play. Game Over.')exit()

CSV file : https://thecompton-my.sharepoint.com/:x:/g/personal/001422_thecompton_org_uk/EbhI4A12pg1EhMezOR8tOlgBF-iOh8JTAM3x3WUOk3i9Ig?e=AJktHi

Answer

Here is what you want

First you had wrong login logic I changed the file name for my convinience

import csvlogin = False
answer = input("Do you have an account?(yes or no) ")if answer == 'yes' :with open('upassword.csv', 'r') as csvfile:csv_reader = csv.reader(csvfile)username = input("Player One Username: ")password = input("Player One Password: ")for row in csv_reader:print(row[0], row[1])print(username, password)if row[0]== username and row[1] == password:login = Truebreakelse:login = Falsebreakif login == True:print("You are now logged in!")else:print("Incorrect. Game Over.")exit()    
else:print('Only Valid Usernames can play. Game Over.')exit()

I have also some print statements to help you understand the workflow

And try to avoid opening files like this

csvfile = open("Username password.csv","r")
https://en.xdnf.cn/q/120087.html

Related Q&A

adding validation to answer in quiz gives wrong answers

I am a complete novice with Python and working on a multiple choice quiz that reads questions from a file and keeps a score that then writes to a file. Everything was working perfectly until I added v…

Why do I get None as the output from a print statement? [duplicate]

This question already has answers here:Why is "None" printed after my functions output?(7 answers)Closed 2 years ago.def print_name(name):print(name)print(print_name(Annabel Lee))Why do I ge…

How to collect tweets about an event that are posted on specific date using python?

I wish to collect all tweets containing specific keywords(ex:manchesterattack,manchester) that are posted about the manchester attack from 22may. Can anyone provide me a code to collect tweets using py…

Pivoting a One-Hot-Encode Dataframe

I have a pandas dataframe that looks like this:genres.head()Drama Comedy Action Crime Romance Thriller Adventure Horror Mystery Fantasy ... History Music War Documentary Sport Musical W…

How to declare multiple similar variables in python? [duplicate]

This question already has answers here:How do I create variable variables?(18 answers)Closed 5 years ago.How can I declare multiple (about 50) variables that count from slider1 to slider50 ? Is there…

what does means this error broken pipe? [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:TCP client-server SIGPIPE I would like know what does this error mean?

Apply a function to each element of a pandas series

I am trying to tokenize each sentence of my pandas series. I try to do as I see in the documentation, using apply, but didnt work:x.apply(nltk.word_tokenize)If I just use nltk.word_tokenize(x) didnt wo…

ValueError: could not convert string to float: in Python 3.10

When someone writes a string or a letter, I want the code make them go back, and the code to print "must be a number and bigger than 0 and less than 100", but what actually happens is the cod…

How do I access Class fields in Python Graph-Tool property maps?

Im trying to draw a graph with a class as a vertex property. How do I draw the graph with the vertex_text set to the name field of the classes they contain?from graph_tool.all import *class Node(objec…

How to iterate through each line of a text file and get the sentiment of those lines using python?

Currently, Im working on Sentiment Analysis part. For this I have preferred to use Standford Core NLP library using python. Im able to get the sentiment for each sentence using the following code : fro…