Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with Python program I have the following program which is meant to build a user dataframe from user input and remember the data

Need help with Python program

I have the following program which is meant to build a user dataframe from user input and remember the data each time the program is run. It's working fine with remembering the usernames, but I have 2 main issues:

1. I want users to also be able to include a password and Password to be another column name in the Dataframe. I cannot figure out how to accomplish this.

Users can have matching passwords but NOT duplicate usernames.

2. I'm opening the file in r+ mode, but this requires the "userf" file to be already existing. I want this file to instead be automatically created. I already tried r+x but this gave me an error that only 1 mode can be used

Here is my code:

import pandas as pd

usernames = []

def myFunc(): with open("userf", "r+") as f: for line in f: usernames.append(line.strip())

while True: username = input("add a username to the usernames: ").lower().strip() if username not in usernames: usernames.append(username) f.write(username + " ") else: print("username already exists, try again") continue while True: ans = input("Add another user? Y/N ").lower().strip() if ans not in ['yes', 'y', 'no', 'n']: print("Please answer y ") else: break if ans == "yes" or ans == "y": continue else: break

user_dictionary = { "Username":usernames }

mydb = pd.DataFrame(user_dictionary) print(mydb) mydb.to_json("database.json")

try: with open("database.json") as saveddb: database = pd.read_json(saveddb) except FileNotFoundError: myFunc() else: print(database) myFunc()

image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Find the derivative of y= cos cos (x + 2x)

Answered: 1 week ago