Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pip install pretty table
- import csv
- from prettytable import from_csv
- def printDb():
- with open('db.csv','r', newline='') as fo:
- with open('db.csv','r') as fo
- x = from_csv(fo)
- print(x)
- #uncomment the below if you don't have prettytable and comment the previous 3 lines
- # reader = csv.reader(fo)
- # next(reader)
- # print("FirstName\tLastname\tAge\tNationality")
- # for line in reader:
- # print(line[0] + "\t\t" + line[1] + "\t\t" + line[2] + "\t" + line[3])
- def addToDb(fname,lname,age,nationality):
- with open('db.csv', 'a', newline='') as fo:
- writer = csv.writer(fo)
- writer.writerow([fname,lname,age,nationality])
- def displayMenu():
- choice = input("1. Display Current Database\n2. Add New Person to Database")
- while int(choice) < 1 or int(choice) > 2:
- choice = input("Please choose either 1 or 2")
- if int(choice) == 1:
- printDb()
- elif int(choice) == 2:
- takeInput()
- def takeInput():
- fname = input("Enter First Name: ")
- lname = input("Enter Last Name: ")
- age = input("Enter Age: ")
- nationality = input("Enter Nationality: ")
- addToDb(fname,lname,age,nationality)
- while True:
- displayMenu()
- retry = input("Press N to quit or any key to go back to homescreen: ").upper()
- if retry == "N":
- break
Advertisement
Add Comment
Please, Sign In to add comment