SlideShare a Scribd company logo
https://www.liveexamhelper.com/
support@liveexamhelper.com
+1 (315) 557-6473 4112 Thrash Trail,
Purdon, Texas-
76679
Problem 1 - Sorting a List
Write a program that asks the user to enter 10 (positive) numbers. The
program should then print the numbers in sorted order, from biggest to
smallest.
To do this, first write a function that takes a list and finds the largest
element. It then
1) deletes that element from the list and
2) returns that element.
Hint: You will need to store two variables in this function: the biggest
number you've seen so far (remember to initially set this to 0), and its
position. Then iterate over the list, and for each element, check if it's bigger
than the biggest number you've seen so far. If it is, change both variables
(remember to change BOTH)!
So in your main program, you'll have to keep calling this function (in a loop)
until the list is empty and keep printing the number that is returned.
# sorting.py
# Example solution for Lab 5, problem 1
# Aseem Kishore
# 6.189 - Intro to Python
# IAP 2008 - Class 4
L = []
for i in range(10):
a = int(raw_input("Please enter a number. "))
# DON'T do this: L[i] = a
L.append(a)
def find_largest(L):
maximum = 0
index = 0
for i in range(len(L)):
if L[i] > maximum:
maximum = L[i]
index = i
del L[index]
return maximum
# this is fine: while len(L) > 0:
for i in range(10):
print find_largest(L),
Problem 2 – Report card with GPA
Write a program where the user can enter each of his grades, after which the
program prints out a report card with GPA. Remember to ask the user how
many classes he took. Example output is below.
How many classes did you take? 4
What was the name of this class? English
What was your grade? 94
...
REPORT CARD:
English - 94
Math – 96
Science – 91
Social Studies - 88
Overall GPA – 92.25
Hints: You’ll want to use a for loop, and you’ll probably want to keep
two lists, one for names and one for grades. Remember, add to lists with
append.
# reportcard.py
# Example solution for Lab 5, problem 2
# Aseem Kishore
# 6.189 - Intro to Python
# IAP 2008 - Class 4
# Helper function that takes a list of grades and returns the GPA.
# Remember that average (mean) is the sum divided by the number of grades.
# Just like in math, you sum first, THEN divide -- order of operations.
# Don't make the mistake of dividing inside the loop!
def calculate_gpa(grades):
running_sum = 0
for grade in grades:
running_sum = running_sum + grade
return float(running_sum) / len(grades) # remember we want decimals, so
# use float(...)
# Main program code
class_names = []
class_grades = []
number_classes = int(raw_input("How many classes did you take? "))
print # this prints a blank line
# Now we're going to ask the same two questions over and over --> loop!
# Since we know how many times we're looping (number_classes), we use for.
# Note that we need a variable name for the "for", but we don't use it here.
# What does range return? A list of numbers, from 0 to number_classes.
for arbitrary_variable_name in range(number_classes):
name = raw_input("What is the name of this class? ")
grade = int(raw_input("What grade did you get? "))
class_names.append(name) # add the two things to our lists...
class_grades.append(grade)
print # blank line
# Now we'll print the report card. The report card should look like:
# class name - grade
# Over and over again --> loop! How many of these lines? number_classes
print "Report card:"
print
for class_number in range(number_classes):
print class_names[class_number], "-", class_grades[class_number]
print
print "Term GPA:", calculate_gpa(class_grades)

More Related Content

Similar to Python Exam (Questions with Solutions Done By Live Exam Helper Experts)

Python basics
Python basicsPython basics
Python basics
Fraboni Ec
 
Python basics
Python basicsPython basics
Python basics
James Wong
 
Python basics
Python basicsPython basics
Python basics
Tony Nguyen
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
Programming Homework Help
 
Python.pptx
Python.pptxPython.pptx
5 structured programming
5 structured programming 5 structured programming
5 structured programming hccit
 
Assg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docx
Assg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docxAssg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docx
Assg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docx
festockton
 
1 CS110 - Introduction to Computers and Applications .docx
1  CS110 - Introduction to Computers and Applications .docx1  CS110 - Introduction to Computers and Applications .docx
1 CS110 - Introduction to Computers and Applications .docx
honey725342
 
DATA STRUCTURE.pdf
DATA STRUCTURE.pdfDATA STRUCTURE.pdf
DATA STRUCTURE.pdf
ibrahim386946
 
DATA STRUCTURE
DATA STRUCTUREDATA STRUCTURE
DATA STRUCTURE
RobinRohit2
 
The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210
Mahmoud Samir Fayed
 
Python introduction
Python introductionPython introduction
Python introduction
leela rani
 
Lab6: I/O and Arrays
Lab6: I/O and ArraysLab6: I/O and Arrays
Lab6: I/O and Arrays
enidcruz
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
DRVaibhavmeshram1
 
Java conceptual learning material
Java conceptual learning materialJava conceptual learning material
Java conceptual learning material
ArthyR3
 
CS10 Python Programming Homework 4 40 points Lists, Tupl.docx
CS10 Python Programming Homework 4 40 points Lists, Tupl.docxCS10 Python Programming Homework 4 40 points Lists, Tupl.docx
CS10 Python Programming Homework 4 40 points Lists, Tupl.docx
mydrynan
 
if, while and for in Python
if, while and for in Pythonif, while and for in Python
if, while and for in Python
PranavSB
 

Similar to Python Exam (Questions with Solutions Done By Live Exam Helper Experts) (20)

Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
5 structured programming
5 structured programming 5 structured programming
5 structured programming
 
Assg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docx
Assg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docxAssg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docx
Assg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docx
 
1 CS110 - Introduction to Computers and Applications .docx
1  CS110 - Introduction to Computers and Applications .docx1  CS110 - Introduction to Computers and Applications .docx
1 CS110 - Introduction to Computers and Applications .docx
 
DATA STRUCTURE.pdf
DATA STRUCTURE.pdfDATA STRUCTURE.pdf
DATA STRUCTURE.pdf
 
DATA STRUCTURE
DATA STRUCTUREDATA STRUCTURE
DATA STRUCTURE
 
The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210
 
Python introduction
Python introductionPython introduction
Python introduction
 
Lab6: I/O and Arrays
Lab6: I/O and ArraysLab6: I/O and Arrays
Lab6: I/O and Arrays
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
Java conceptual learning material
Java conceptual learning materialJava conceptual learning material
Java conceptual learning material
 
CS10 Python Programming Homework 4 40 points Lists, Tupl.docx
CS10 Python Programming Homework 4 40 points Lists, Tupl.docxCS10 Python Programming Homework 4 40 points Lists, Tupl.docx
CS10 Python Programming Homework 4 40 points Lists, Tupl.docx
 
if, while and for in Python
if, while and for in Pythonif, while and for in Python
if, while and for in Python
 

More from Live Exam Helper

Nursing Exam Help
Nursing Exam HelpNursing Exam Help
Nursing Exam Help
Live Exam Helper
 
Statistical Physics Exam Help
Statistical Physics Exam HelpStatistical Physics Exam Help
Statistical Physics Exam Help
Live Exam Helper
 
Take My Nursing Exam
Take My Nursing ExamTake My Nursing Exam
Take My Nursing Exam
Live Exam Helper
 
Pay for economics exam
Pay for economics  examPay for economics  exam
Pay for economics exam
Live Exam Helper
 
Take My Economics Exam
Take My Economics ExamTake My Economics Exam
Take My Economics Exam
Live Exam Helper
 
Best Economics Exam Help
Best Economics Exam HelpBest Economics Exam Help
Best Economics Exam Help
Live Exam Helper
 
Take My Accounting Exam
Take My Accounting ExamTake My Accounting Exam
Take My Accounting Exam
Live Exam Helper
 
Microeconomics Exam Questions and Answers
Microeconomics Exam Questions and AnswersMicroeconomics Exam Questions and Answers
Microeconomics Exam Questions and Answers
Live Exam Helper
 
Exam Questions and Solutions of Molecular Biology
Exam Questions and Solutions of Molecular BiologyExam Questions and Solutions of Molecular Biology
Exam Questions and Solutions of Molecular Biology
Live Exam Helper
 
Probabilistic Methods of Signal and System Analysis Solutions
Probabilistic Methods of Signal and System Analysis SolutionsProbabilistic Methods of Signal and System Analysis Solutions
Probabilistic Methods of Signal and System Analysis Solutions
Live Exam Helper
 
Digital Communication Exam Help
Digital Communication Exam HelpDigital Communication Exam Help
Digital Communication Exam Help
Live Exam Helper
 
Digital Communication Exam Help
Digital Communication Exam HelpDigital Communication Exam Help
Digital Communication Exam Help
Live Exam Helper
 
Digital Communication Exam Help
Digital Communication Exam HelpDigital Communication Exam Help
Digital Communication Exam Help
Live Exam Helper
 
Continuum Electromechanics Exam Help
Continuum Electromechanics Exam HelpContinuum Electromechanics Exam Help
Continuum Electromechanics Exam Help
Live Exam Helper
 
Continuum Electromechanics Exam Help
Continuum Electromechanics Exam HelpContinuum Electromechanics Exam Help
Continuum Electromechanics Exam Help
Live Exam Helper
 
Electromechanics Exam Help
Electromechanics Exam HelpElectromechanics Exam Help
Electromechanics Exam Help
Live Exam Helper
 
Materials Science Exam Help
Materials Science Exam HelpMaterials Science Exam Help
Materials Science Exam Help
Live Exam Helper
 
Nuclear Engineering Exam Help
Nuclear Engineering Exam HelpNuclear Engineering Exam Help
Nuclear Engineering Exam Help
Live Exam Helper
 
Engineering Management Exam Help
Engineering Management Exam HelpEngineering Management Exam Help
Engineering Management Exam Help
Live Exam Helper
 
online chemistry exam help
online chemistry exam helponline chemistry exam help
online chemistry exam help
Live Exam Helper
 

More from Live Exam Helper (20)

Nursing Exam Help
Nursing Exam HelpNursing Exam Help
Nursing Exam Help
 
Statistical Physics Exam Help
Statistical Physics Exam HelpStatistical Physics Exam Help
Statistical Physics Exam Help
 
Take My Nursing Exam
Take My Nursing ExamTake My Nursing Exam
Take My Nursing Exam
 
Pay for economics exam
Pay for economics  examPay for economics  exam
Pay for economics exam
 
Take My Economics Exam
Take My Economics ExamTake My Economics Exam
Take My Economics Exam
 
Best Economics Exam Help
Best Economics Exam HelpBest Economics Exam Help
Best Economics Exam Help
 
Take My Accounting Exam
Take My Accounting ExamTake My Accounting Exam
Take My Accounting Exam
 
Microeconomics Exam Questions and Answers
Microeconomics Exam Questions and AnswersMicroeconomics Exam Questions and Answers
Microeconomics Exam Questions and Answers
 
Exam Questions and Solutions of Molecular Biology
Exam Questions and Solutions of Molecular BiologyExam Questions and Solutions of Molecular Biology
Exam Questions and Solutions of Molecular Biology
 
Probabilistic Methods of Signal and System Analysis Solutions
Probabilistic Methods of Signal and System Analysis SolutionsProbabilistic Methods of Signal and System Analysis Solutions
Probabilistic Methods of Signal and System Analysis Solutions
 
Digital Communication Exam Help
Digital Communication Exam HelpDigital Communication Exam Help
Digital Communication Exam Help
 
Digital Communication Exam Help
Digital Communication Exam HelpDigital Communication Exam Help
Digital Communication Exam Help
 
Digital Communication Exam Help
Digital Communication Exam HelpDigital Communication Exam Help
Digital Communication Exam Help
 
Continuum Electromechanics Exam Help
Continuum Electromechanics Exam HelpContinuum Electromechanics Exam Help
Continuum Electromechanics Exam Help
 
Continuum Electromechanics Exam Help
Continuum Electromechanics Exam HelpContinuum Electromechanics Exam Help
Continuum Electromechanics Exam Help
 
Electromechanics Exam Help
Electromechanics Exam HelpElectromechanics Exam Help
Electromechanics Exam Help
 
Materials Science Exam Help
Materials Science Exam HelpMaterials Science Exam Help
Materials Science Exam Help
 
Nuclear Engineering Exam Help
Nuclear Engineering Exam HelpNuclear Engineering Exam Help
Nuclear Engineering Exam Help
 
Engineering Management Exam Help
Engineering Management Exam HelpEngineering Management Exam Help
Engineering Management Exam Help
 
online chemistry exam help
online chemistry exam helponline chemistry exam help
online chemistry exam help
 

Recently uploaded

Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 

Recently uploaded (20)

Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 

Python Exam (Questions with Solutions Done By Live Exam Helper Experts)

  • 2. Problem 1 - Sorting a List Write a program that asks the user to enter 10 (positive) numbers. The program should then print the numbers in sorted order, from biggest to smallest. To do this, first write a function that takes a list and finds the largest element. It then 1) deletes that element from the list and 2) returns that element. Hint: You will need to store two variables in this function: the biggest number you've seen so far (remember to initially set this to 0), and its position. Then iterate over the list, and for each element, check if it's bigger than the biggest number you've seen so far. If it is, change both variables (remember to change BOTH)! So in your main program, you'll have to keep calling this function (in a loop) until the list is empty and keep printing the number that is returned.
  • 3. # sorting.py # Example solution for Lab 5, problem 1 # Aseem Kishore # 6.189 - Intro to Python # IAP 2008 - Class 4 L = [] for i in range(10): a = int(raw_input("Please enter a number. ")) # DON'T do this: L[i] = a L.append(a) def find_largest(L): maximum = 0 index = 0
  • 4. for i in range(len(L)): if L[i] > maximum: maximum = L[i] index = i del L[index] return maximum # this is fine: while len(L) > 0: for i in range(10): print find_largest(L),
  • 5. Problem 2 – Report card with GPA Write a program where the user can enter each of his grades, after which the program prints out a report card with GPA. Remember to ask the user how many classes he took. Example output is below. How many classes did you take? 4 What was the name of this class? English What was your grade? 94 ... REPORT CARD: English - 94 Math – 96 Science – 91 Social Studies - 88 Overall GPA – 92.25
  • 6. Hints: You’ll want to use a for loop, and you’ll probably want to keep two lists, one for names and one for grades. Remember, add to lists with append. # reportcard.py # Example solution for Lab 5, problem 2 # Aseem Kishore # 6.189 - Intro to Python # IAP 2008 - Class 4 # Helper function that takes a list of grades and returns the GPA. # Remember that average (mean) is the sum divided by the number of grades. # Just like in math, you sum first, THEN divide -- order of operations. # Don't make the mistake of dividing inside the loop! def calculate_gpa(grades): running_sum = 0 for grade in grades:
  • 7. running_sum = running_sum + grade return float(running_sum) / len(grades) # remember we want decimals, so # use float(...) # Main program code class_names = [] class_grades = [] number_classes = int(raw_input("How many classes did you take? ")) print # this prints a blank line # Now we're going to ask the same two questions over and over --> loop! # Since we know how many times we're looping (number_classes), we use for. # Note that we need a variable name for the "for", but we don't use it here. # What does range return? A list of numbers, from 0 to number_classes. for arbitrary_variable_name in range(number_classes): name = raw_input("What is the name of this class? ") grade = int(raw_input("What grade did you get? "))
  • 8. class_names.append(name) # add the two things to our lists... class_grades.append(grade) print # blank line # Now we'll print the report card. The report card should look like: # class name - grade # Over and over again --> loop! How many of these lines? number_classes print "Report card:" print for class_number in range(number_classes): print class_names[class_number], "-", class_grades[class_number] print print "Term GPA:", calculate_gpa(class_grades)