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

Page 1

PYTHON EXAM (QUESTIONS WITH SOLUTIONS DONE BY LIVE EXAM HELPER EXPERTS) https://www.liveexamhelper.com/ support@liveexamhelper.com +1 (315) 557-6473 4112 Thrash Trail, Purdon, Texas76679

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.

https://www.liveexamhelper.com/

# 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 https://www.liveexamhelper.com/

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), https://www.liveexamhelper.com/

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 https://www.liveexamhelper.com/

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
the number of grades. # Just
math, you sum
THEN divide
of operations. # Don't
dividing
loop! def calculate_gpa(grades): running_sum = 0 for grade in grades: https://www.liveexamhelper.com/
by
like in
first,
-- order
make the mistake of
inside the

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? ")) https://www.liveexamhelper.com/

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) https://www.liveexamhelper.com/

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.