SlideShare a Scribd company logo
1 of 8
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)

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.docxfestockton
 
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 .docxhoney725342
 
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 210Mahmoud Samir Fayed
 
Python introduction
Python introductionPython introduction
Python introductionleela rani
 
Lab6: I/O and Arrays
Lab6: I/O and ArraysLab6: I/O and Arrays
Lab6: I/O and Arraysenidcruz
 
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 materialArthyR3
 
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.docxmydrynan
 
if, while and for in Python
if, while and for in Pythonif, while and for in Python
if, while and for in PythonPranavSB
 

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

Statistical Physics Exam Help
Statistical Physics Exam HelpStatistical Physics Exam Help
Statistical Physics Exam HelpLive Exam Helper
 
Microeconomics Exam Questions and Answers
Microeconomics Exam Questions and AnswersMicroeconomics Exam Questions and Answers
Microeconomics Exam Questions and AnswersLive 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 BiologyLive 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 SolutionsLive Exam Helper
 
Digital Communication Exam Help
Digital Communication Exam HelpDigital Communication Exam Help
Digital Communication Exam HelpLive Exam Helper
 
Digital Communication Exam Help
Digital Communication Exam HelpDigital Communication Exam Help
Digital Communication Exam HelpLive Exam Helper
 
Digital Communication Exam Help
Digital Communication Exam HelpDigital Communication Exam Help
Digital Communication Exam HelpLive Exam Helper
 
Continuum Electromechanics Exam Help
Continuum Electromechanics Exam HelpContinuum Electromechanics Exam Help
Continuum Electromechanics Exam HelpLive Exam Helper
 
Continuum Electromechanics Exam Help
Continuum Electromechanics Exam HelpContinuum Electromechanics Exam Help
Continuum Electromechanics Exam HelpLive Exam Helper
 
Electromechanics Exam Help
Electromechanics Exam HelpElectromechanics Exam Help
Electromechanics Exam HelpLive Exam Helper
 
Materials Science Exam Help
Materials Science Exam HelpMaterials Science Exam Help
Materials Science Exam HelpLive Exam Helper
 
Nuclear Engineering Exam Help
Nuclear Engineering Exam HelpNuclear Engineering Exam Help
Nuclear Engineering Exam HelpLive Exam Helper
 
Engineering Management Exam Help
Engineering Management Exam HelpEngineering Management Exam Help
Engineering Management Exam HelpLive Exam Helper
 
online chemistry exam help
online chemistry exam helponline chemistry exam help
online chemistry exam helpLive 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

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 

Recently uploaded (20)

Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 

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)