You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#Like the admins above is a dictionary but of students. Dictionaries use curly brackets with colons to associate keys with values. In this case, each student's first name is a key. The values are lists of grades.
13
-
#Lists are denoted with square brackets. Values are indexed within starting with 0 for the first one. Each value is separated by commas.
14
+
# Like the admins above is a dictionary but of students.
15
+
# Dictionaries use curly brackets with colons to associate keys with values.
16
+
# In this case, each student's first name is a key. The values are lists of grades.
17
+
# Lists are denoted with square brackets.
18
+
# Values are indexed within starting with 0 for the first one.
19
+
# Each value is separated by commas.
14
20
students= {'Alex':[87,88,98],
15
21
'Sally':[88,67,93],
16
22
'Nboke':[90,88,78]}
17
23
18
-
#Now we define functions. Functions encapsulate logic into reusable recipes that can be executed whenever we need them by calling their name with parentheses.
19
-
defenterGrades():
20
-
nameToEnter=input('Student name: ')
21
-
gradeToEnter=input('Grade: ')
22
-
#This checks through the keys of the students dictionary to see if the name entered exactly matches any one in there.
23
-
ifnameToEnterinstudents:
24
-
print('Adding grade for'+nameToEnter)
25
-
students[nameToEnter].append(float(gradeToEnter)) #float will have a .0
26
-
print(str(nameToEnter)+' now has these grades:')
27
-
print(students[nameToEnter])
24
+
# Now we define functions. Functions encapsulate logic into reusable recipes that can be
25
+
# executed whenever we need them by calling their name with parentheses.
26
+
defenter_grades():
27
+
'''
28
+
Function to input a grade for a given student.
29
+
'''
30
+
name_to_enter=input('Student name: ')
31
+
grade_to_enter=input('Grade: ')
32
+
# This checks through the keys of the students dictionary to see if the name entered
33
+
# exactly matches any one in there.
34
+
ifname_to_enterinstudents:
35
+
print('Adding grade for'+name_to_enter)
36
+
students[name_to_enter].append(float(grade_to_enter)) #float will have a .0
37
+
print(str(name_to_enter)+' now has these grades:')
38
+
print(students[name_to_enter])
28
39
else:
29
40
print('Student not found. Please check your spelling or go back and add if new.')
30
41
31
-
defremoveStudent():
32
-
nameToRemove=input('Who do you want to remove? ')
33
-
ifnameToRemoveinstudents:
34
-
print('Removing '+nameToRemove)
35
-
delstudents[nameToRemove]
42
+
defremove_student():
43
+
'''
44
+
Function to remove a specific student.
45
+
'''
46
+
name_to_remove=input('Who do you want to remove? ')
47
+
ifname_to_removeinstudents:
48
+
print('Removing '+name_to_remove)
49
+
delstudents[name_to_remove]
36
50
print(students)
37
51
else:
38
52
print('Student not found.')
39
53
40
-
defaverageStudents():
54
+
defaverage_students():
55
+
'''
56
+
Function to average all students' grades.
57
+
'''
41
58
forstudentinstudents:
42
59
grades=students[student]
43
-
average=s.mean(grades) #notice the s? we imported the statistuics module as s. Thus, we are using a fucntion called "mean()" from the statistics module.
60
+
average=s.mean(grades) #notice the s? we imported the statistics module as s.
61
+
#Thus, we are using a fucntion called "mean()" from the statistics module.
44
62
print(student,' average ',average)
45
63
46
64
defmain():
65
+
'''
66
+
Function to show user main menu and process option selections.
67
+
'''
47
68
print("User: "+login)
48
-
#Here we present our main menu options once a person logs in successfully.
69
+
#Here we present our main menu options once a person logs in successfully.
49
70
print("""
50
71
Welcome to the Grade Tracker
51
72
@@ -59,20 +80,20 @@ def main():
59
80
#Here we process their choice of what they want to do.
60
81
ifaction=='1':
61
82
#print('1 selected')
62
-
enterGrades()
83
+
enter_grades()
63
84
elifaction=='2':
64
85
#print('2 selected')
65
-
removeStudent()
86
+
remove_student()
66
87
elifaction=='3':
67
88
#print('3 selected')
68
-
averageStudents()
89
+
average_students()
69
90
elifaction=='4':
70
91
#print('4 selected')
71
92
exit()
72
93
else:
73
94
print('Valid option not selected.') #need to cause it to reprompt
Copy file name to clipboardExpand all lines: 4 DB in Python/SQLite Demo/vinventory.py
+2-1Lines changed: 2 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -10,8 +10,9 @@
10
10
#if you code is not connecting to the DB, uncomment the next three lines and read the comments. Also, you may need \ instead of / before the DB file name in windows
11
11
importos
12
12
path_root=os.path.dirname(os.path.abspath(__file__)) #grab the file system path to the current script file
13
-
print("Here is the PATH_ROOT",path_root)
13
+
#print("Here is the PATH_ROOT",path_root)
14
14
database_file_path=str(path_root)+"/myinventory.db"#construct the path to the database file (only necessary if the current working directory is not the same as the folder where this Python file is located.)
15
+
print("HERE is the database file:", database_file_path)
15
16
#if you uncomment the three lines above, be sure to comment out this next line
0 commit comments