For any Assignment related queries, Call us at : -
+1 678 648 4277
You can mail us at : -
[email protected] or
reach us at : - www.pythonhomeworkhelp.com/
Python Homework Help
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.
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.
Python Homework Help
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.
Python Homework Help
Solutions:
# 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),
Python Homework Help
# 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(...)
Python Homework Help
# 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):
Python Homework Help
print
class_names[class_nu
mber], "-",
class_grades[class_nu
mber]
print
print "Term GPA:",
calculate_gpa(class_gr
ades)
Python Homework Help