-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathsetup_database.py
More file actions
111 lines (73 loc) · 2.38 KB
/
setup_database.py
File metadata and controls
111 lines (73 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 29 12:25:45 2025
@author: serenabonaretti
This module contains functions to create usernames and passwords
"""
import random
def create_username (first_name, last_name):
"""Create a lowercase username made of initial of first name and last name
Parameters
----------
first_name: string
First name of a person
last_name: string
Last name of a person
Returns
-------
username: string
Created username
"""
# concatenate initial of first name and last name
username = first_name[0] + last_name
# make sure username is lowercase
username = username.lower()
# return username
return username
def create_password():
"""Create a password composed of four random integers
Returns
-------
password: string
Created password
"""
# create a random number with four digits
password = str(random.randint(1000,9999))
# return password
return password
def create_database (customers):
"""Creates a database as a dictionary with usernames as keys and passwords as values
Parameters
----------
customers : list of lists
Each sublist contains first name and last name of a customer
Returns
-------
db : dictionary
Created database (shorted as db)
n_customers : int
Number of customers in the database
"""
# initialize dictionary (i.e. database)
db = {}
# for each customer
for customer in customers:
# create username
username = create_username (customer[0], customer[1])
# create password
password = create_password()
# add username and password to db
db[username] = password
# compute number of customers
n_customers = len(db)
# return dictionary and its length
return db, n_customers
if __name__ == "__main__":
# input to the main function
customers = [["Maria", "Lopez"], ["Julia", "Smith"], ["Mohammed", "Seid"]]
# create the database
database, number_customers = create_database(customers)
# print the outputs
print ("Database:", database)
print ("Number of customers:", number_customers)