|
| 1 | +''' |
| 2 | +This is a grade tracking program - think through the goal up front- what is the task and design? |
| 3 | +needs to enable several basic functions for faculty members. |
| 4 | +This comment in triple quotes is called a docstring. |
| 5 | +It is text that explains what a code file (aka module) or function does. |
| 6 | +See https://www.geeksforgeeks.org/python-docstrings/ |
| 7 | +This version saves the login and password in a separate file. |
| 8 | +You can learn more about how to handle secrets like passwords here: |
| 9 | +https://blog.gitguardian.com/how-to-handle-secrets-in-python/ |
| 10 | +''' |
| 11 | +#import libraries (aka module) first |
| 12 | +import statistics as s |
| 13 | +from dotenv import load_dotenv |
| 14 | +import os |
| 15 | + |
| 16 | +#Now we can load the login and password from the .env file. They are no longer exposed in the code. |
| 17 | +load_dotenv() |
| 18 | +admins = os.getenv("admins") |
| 19 | +students = os.getenv("students") |
| 20 | + |
| 21 | + |
| 22 | +# Now we define functions. Functions encapsulate logic into reusable recipes that can be |
| 23 | +# executed whenever we need them by calling their name with parentheses. |
| 24 | +def enter_grades(): |
| 25 | + ''' |
| 26 | + Function to input a grade for a given student. |
| 27 | + ''' |
| 28 | + name_to_enter = input('Student name: ') |
| 29 | + grade_to_enter = input('Grade: ') |
| 30 | + # This checks through the keys of the students dictionary to see if the name entered |
| 31 | + # exactly matches any one in there. |
| 32 | + if name_to_enter in students: |
| 33 | + print('Adding grade for'+name_to_enter) |
| 34 | + students[name_to_enter].append(float(grade_to_enter)) #float will have a .0 |
| 35 | + print(str(name_to_enter)+' now has these grades:') |
| 36 | + print(students[name_to_enter]) |
| 37 | + else: |
| 38 | + print('Student not found. Please check your spelling or go back and add if new.') |
| 39 | + |
| 40 | +def remove_student(): |
| 41 | + ''' |
| 42 | + Function to remove a specific student. |
| 43 | + ''' |
| 44 | + name_to_remove = input('Who do you want to remove? ') |
| 45 | + if name_to_remove in students: |
| 46 | + print('Removing '+name_to_remove) |
| 47 | + del students[name_to_remove] |
| 48 | + print(students) |
| 49 | + else: |
| 50 | + print('Student not found.') |
| 51 | + |
| 52 | +def average_students(): |
| 53 | + ''' |
| 54 | + Function to average all students' grades. |
| 55 | + ''' |
| 56 | + for student in students: |
| 57 | + grades = students[student] |
| 58 | + average = s.mean(grades) #notice the s? we imported the statistics module as s. |
| 59 | + #Thus, we are using a fucntion called "mean()" from the statistics module. |
| 60 | + print(student,' average ',average) |
| 61 | + |
| 62 | +def main(): |
| 63 | + ''' |
| 64 | + Function to show user main menu and process option selections. |
| 65 | + ''' |
| 66 | + print("User: " + login) |
| 67 | + # Here we present our main menu options once a person logs in successfully. |
| 68 | + print(""" |
| 69 | + Welcome to the Grade Tracker |
| 70 | +
|
| 71 | + [1] - Enter Grades |
| 72 | + [2] - Remove Student |
| 73 | + [3] - Student Averages |
| 74 | + [4] - Exit |
| 75 | + """) |
| 76 | + |
| 77 | + action = input('What would you like to do? (Enter a number) ') |
| 78 | + #Here we process their choice of what they want to do. |
| 79 | + if action == '1': |
| 80 | + #print('1 selected') |
| 81 | + enter_grades() |
| 82 | + elif action == '2': |
| 83 | + #print('2 selected') |
| 84 | + remove_student() |
| 85 | + elif action == '3': |
| 86 | + #print('3 selected') |
| 87 | + average_students() |
| 88 | + elif action == '4': |
| 89 | + #print('4 selected') |
| 90 | + exit() |
| 91 | + else: |
| 92 | + print('Valid option not selected.') #need to cause it to reprompt |
| 93 | + |
| 94 | +login = input('Faculty account name: ') |
| 95 | + |
| 96 | +if login in admins: |
| 97 | + password = input('Password: ') |
| 98 | + if admins[login] == password: |
| 99 | + print('Welcome,',login) |
| 100 | + #now run the code |
| 101 | + while True: |
| 102 | + main() |
| 103 | + else: |
| 104 | + print('Invalid password.') |
| 105 | +else: |
| 106 | + print('Invalid user.') |
0 commit comments