Python Tutorial 15: Solution to Python Pickle Homework Example Problems

preview_player
Показать описание
You guys can help me out over at Patreon, and that will help me keep my gear updated, and help me keep this quality content coming:

In this video we show step-by-step instructions on how to work some example problems in python using Pickle. I do not assume you are an expert, so these lessons are designed for complete beginners.
#Python
#Lessons
#Programming
Рекомендации по теме
Комментарии
Автор

I AM LEGEND. I made two separate programs. One to write the student data, the second to read and ask the user input. All of it was made to be non case-sensitive, with a clean layout. Love the realistic challenges you present. Very much a similar challenge to an intro to programming class one would encounter in college!

proteopathy
Автор

Loved this lesson! I folded up like a cheap walmart chair (at least not a dollar general one) but this helped a lot! Thanks Paul.

directhubfeexam
Автор

I did the homework! I am finally a legend. Sometimes I cannot do the homework 100%, I always try to do my best, and suffer. Thanks for pushing us.

aungphyokyaw
Автор

Legend. I'm starting to feel confident enough to add in some loops for things like re-prompting for the number of students if the inputted number is 0 or less than zero, prompting to display the student roster before recalling a student, and similar types of small improvements. I know there's a TON of ways I can optimize the code I'm writing, but I feel like each program I write is getting better, which is a really awesome feeling!

As always, thanks Paul!

ModernDayWanderlust
Автор

First lesson in Python Tutorial where you've asked us to hold our breath. My kids and I have been waiting for it.

raymondwood
Автор

I combined both programs into one with a combination of if /elif and input options. appended a data set and made the counter into a string which became the student ID. Thus could locate the student by student ID or any of the elements in each "appended "list. Really enjoyed this.

jbelmont
Автор

Your 78 year-old student is Legend! Keep up the great work Paul!

cbrombaugh
Автор

I AM LEGEND!!! I was honestly a bit surprised when mine worked the first time around. Thanks, Paul, for all you do for everyone! You're the true legend, sir!

I knew the receiver program would be the toughest part, and it took me about an hour or two to think through. I made an array called 'students' which stores the number of students as the first data type, the names as the second, and the grades as the final one.

As the user inputs them and they are appended, the index of the individual 'grade' within 'grades' lines up with the index of the individual 'name' within 'names'. So I made a for loop to cycle through each name in the names array until it finds the string equal to the input name. Then, if the name is found, it uses that index for the same index as the grade. The program then prints out the student's name with the grade. It made it really easy!

FF
Автор

Paul,

I used a slightly different tack in the second program. Rather than creating an array for numGrade, I handled the indexing like this:
j=0
student=input('Which students average would you like to check? ')
for i in stuArray:
if (student==i):
print(student, 'has an average grade of ', avgArray[j])
j=j+1

RobVollmar
Автор

I AM LEGEND!
I was unsure about where to go for this one, but I took several educated guesses and managed to get it right the first time (ignoring some minor syntax issues).
I did it differently than Mr.McWhorter. Instead of making two arrays (one for the students and one for the grades) I built a single array that includes both. I show the code for my first file below.
I originally used a for-loop in the second program to select out the data. I like this code, so I also posted the entire thing below (It includes good formatting of all printed text.).
Lastly, not posted, I wrote the second program using a while-loop to select the data.
The while(1==1) I added to both of my second programs after watching the video.
Thanks for the great lessons!

File 1: Creates Pickle File

#PaulMcWhorter - Python Beginners
#Lesson 15: Saving and Reading Data Files With Pickle
#The Write File: HWPickleA
#by Jonathan Landers
#May 20, 2023

### Homework: Python File 1 ###
'''
This homework requires two python files.
The 1st python file prompts the user for a list of student names and their average grades.
The program saves this data in a pickle file as an array.
The 2nd python uploads the pickle file and prompts the user for a student name.
If the student name is in the data on the pickle file, the program gives the average grade of that student.
'''

#Import pickle library (included in python download).
import pickle

#Create Data.
StudentNum = int(input('How many students do you have?:'))
StGrad = []
for j in range(0, StudentNum, 1):
x = str(input('What is your students name?: '))
y = float(input("What is this student's average grade?: "))
StGrad.append([x, y])

#Create pickle file and dump data into it.
with open('PaulMcWhorterPythonBeginners15HWPickle.pkl', 'wb') as p:
pickle.dump(StGrad, p)
pickle.dump(StudentNum, p)

#Running the program will creat the pickle file.


File 2: Uses Pickle File

#PaulMcWhorter - Python Beginners
#Lesson 15: Saving and Reading Data Files With Pickle
#The Write File: HWPickleWhile
#by Jonathan Landers
#May 20, 2023

### Homework: Python File 2 ###
'''
This homework requires two python files.
The 1st python file prompts the user for a list of student names and their average grades.
The program saves this data in a pickle file as an array.
The 2nd python uploads the pickle file and prompts the user for a student name.
If the student name is in the data on the pickle file, the program gives the average grade of that student.
'''

#Import pickle library (included in python download).
import pickle

#Upload pickled data.
with open('PaulMcWhorterPythonBeginners15HWPickle.pkl', 'rb') as p:
stgrad = pickle.load(p)
stnum = pickle.load(p)

'''
Paul McWhorter's addition:
We want this part of the program to run forever
so that the user does not need to restart the program every time around.
So, create a while loop that runs so long as 1 = 1, which is always true.
'''
while (1==1):
#Ask user for student name.
nameRequest = str(input('What is your students name?: '))
st = 'nothing'
grad = 0
#Find out if requested name is in pickle file.
#If data is in pickle file, create a variable for the student name and for that student's grade
for i in range (0, stnum, 1):
if(nameRequest == stgrad[i][0]):
st = stgrad[i][0]
grad = stgrad[i][1]
if(nameRequest != stgrad[i][0]):
stnot = 'Name not in the list. Please enter a different name.'
gradnot = int(0)

#Print out the studen't name with their grade if available.
#Otherwise, tell user that the student's name is not in the list.
if(grad != 0 and st != 'nothing'):
ststr = str(st)
gradstr = str(grad)
print('')
print(ststr, 'has an average grade of', gradstr + '.')
print('')
else:
stnotstr = str(stnot)
print('')
print(stnotstr)
print('')

jonathanlanders
Автор

That was cool homework, i enjoyed doing it. It wasn't easy but it was doable!

Love your work!

silver
Автор

"I am legend". But I could only do it after I allowed my brain to go into a different state. At first I had no clue what to do. And then later, inspiration came.
And thanks, I really have a lot of fun!!
Many blessings from South Africa!

EFoxVN
Автор

Very good Paul. I am sixty five years old without any prior coding experience. Found your way of teaching excellent. This particular exercise was tough to me. But i tried my best and could make the first part 'somewhat' a success; though i know that there is no 'somewhat' in this field. Thank you.

rajeevin
Автор

I am legend! easier than the last assignment. Separated it into two programs, one for the input, and one to retrieve the data. got a little messed up at first with the pickle storage, but figured it out.

madsci
Автор

I am legend, I took python last year (currently a Junior in College) but I am amazed at how much you cover in such short time and with such clarity! Just have to strengthen my fundamentals with you

jeantorres
Автор

I AM LEGEND! I did something a little bit different:

While entering the information about a student, I gave them a serial number, their name and their average marks.
While reading, I gave them options to find the students' information using their serial number, name or average marks.

Everything worked perfectly, because I understood everything perfectly, because you teach perfectly!
Thank you for another amazing series.

antrikshfulwani
Автор

I am legend. This took a while for me as i put everything into one list before dumping. happy to see different solutions

nicholastan
Автор

I am a LEGEND PAUL. I was so close to giving up but i wanted to be able to call myself a legend after being a lawn chair for so long so i kept thinking about it and reviewing our past assignments until it finally came to me like a bolt of lightning what to do :D. And now i am proud to say i have become a legend once again

potatoz
Автор

Great lesson Paul. In working through the homework I wanted to be able to leave myself notes in the program to come back to in the future and learned about the "#" to leave comments on lines. I think it would be awesome to mention that in the first few lessons so people can leave themselves notes.

benthompson
Автор

Another great lesson Paul. I've been able to do the homework thus far. While I would say "I am Legend, " I did study this when I went back to school to get a degree in IOT design and prototyping (just so I could build SDR ham radios and teach). It also took me a few tries....Thank for another great lesson! I will pass this on to my students!

hughpatterson
welcome to shbcf.ru