In: Computer Science
Writing python code: Can you do for me this.
The question looks bellow
Overview :
As a programmer, you have be asked by a good friend to create a program that will allow them to keep track of their personal expenses. They want to be able to enter their expenses for the month, store them in a file, and then have the ability to retrieve them later. However, your friend only wants certain people to have the ability to view his or her expenses, so they are requesting that you include some sort of login functionality. Instructions : For this challenge, you will start this application for your friend by concentrating on the login functionality. You will have a standard report in a text file where you will build login functionality to access. Below you will find the step by step instructions on how to develop this application.
1. Declaring Variables - You will start the application by declaring the following variables:
# username - initialized with an empty string to store the username entered by the user.
#password - initialized with an empty string to store the password entered by the user.
#repeat - initialized with the letter 'y' to control the looping of the program'
#passwordLength - intialized with the number 8 to validate password length ,
#Four flags set to false named hasAmpersand, hasValidLength, isValidUsername, and isValidPassword
2. Display the program header as shown in the images below
3. Your program must allow the program to repeat without re-displaying the program header.
4. You must obtain the username from the user then check to see if the username contains the @ symbol. If does, set the hasAmpersand flag to true and allow the program to continue. If it doesn't, alert the user that it is an invalid email and use the continue command to skip the rest of the code and repeat the program.
5. Obtain the user password and use a while loop to make sure that the user has entered a password greater than the number stored in the passwordLength variable. After the password has been validated to be greater than the stated password length, change the hasValidLength flag to true.
6. You will then use the hasAmpersand and hasValidLength flags to confirm whether or not to open the logins.txt file to confirm that this login information is present in the file. This is when you would change the appropriate isValid flags to state that they are in the file. (HINT: You can check first that the username is in the file, if not, then there is no need to check for the password.) If the username is not in the file, use the break statement to end the program as shown below.
7. The report stored in the PersonalExpense.txt file should only be display if both the username and password is correct.
8. The text files for this application are attached to the Dropbox. You may download and add them to your solution. Or feel free to create your own login and report pages. View comments (1)
CODE:
import csv
# dictionary to hold the login information
dict_login={}
# file reader to read the login.txt
reader = csv.reader(open('login.txt'))
for row in reader:
key = row[0]
if key in dict_login:
# implement your duplicate row handling here
pass
dict_login[key] = row[1]
# initial instruction
print('Please enter the username, with email id')
print('Please enter the password, having length greater than 8')
# method to take inputs and perform business logic
def takeInputs():
cntr=0
# continue till user explicitly exits
while(True):
cntr+=1
# if visiting second time
if cntr>1:
print("Want to login again?, press y to continue")
y=input()
if y.strip().lower()!="y":
break
username=input("Please enter username").strip()
password=input("Please enter password").strip()
hasAmpersand=False
hasValidLength=False
isValidUsername=False
isValidPassword=False
#if username contains @
if "@" in username:
hasAmpersand=True
# if passowrd length is greter than 8
if len(password)>=8:
hasValidLength=True
# if username is present in the login dictionary
if username in dict_login:
isValidUsername=True
# if password mathces
if password==dict_login[username]:
isValidPassword=True
# logic to display the expense report
filehandle = open("PersonalExpense.txt", 'r')
while True:
# read a single line
line = filehandle.readline()
if not line:
break
print(line)
filehandle.close()
# failed condition to password
else:
print("Supplied password was wrong")
continue
# failed username presence
else:
print("Provided username is not present in our records")
continue
# failed pasword length criteria
else:
print("password length criteria not met, flow will restart")
continue
# failed username presence
else:
print("Not a valid username, flow will restart")
continue
takeInputs()
FILES:
login.text
username,password
[email protected],ertyghyui
[email protected],machine54
[email protected],ertyghyui
[email protected],machine54
[email protected],ertyghyui
[email protected],machine54
[email protected],ertyghyui
[email protected],machine54
PersonalExpense.txt:
Some random information related to the expense
SNIPPET:
RESULT: