CmSc150 Fundamentals of Computing I

advertisement
CmSc150 Fundamentals of Computing I
Homework 04 due 09/24
name_________________
This assignment consists of 2 parts – problem solving and programming. Download this
document, save it as your_nameHW04.doc. Solve the problems in Part 1, implement the two
programs in Part 2, create a folder named your_nameHW04, copy (or move) there the three
files – the WORD file, and the two Python programs, zip the folder and upload in Scholar.
Part 1: Problems
1. Write the if-statements to perform the following:
print “case 1” if a number x is exactly divisible by both 2 and 3
print “case 2” if a number x is exactly divisible by 2 but not by 3
print “case 3” if a number x is exactly divisible by 3 but not by 2
print “case 4” if a number x is not exactly divisible by either 2 or 3
2. The following code is part of a program that plays the guessing game: the computer
generates a number and the player has to guess it. However, there is a logical error, find it
and fix it. The number generated by the computer is stored in the variable toGuess:
done = False
guess = int(input("Enter your guess: "))
while not done:
if toGuess == guess:
print("Congrats!!! You did it!")
done = True
elif toGuess > guess:
print("Guess higher...")
else:
print("Guess lower...")
3. Solve this problem first on paper and then check your solutions in Python.
What would be output of the following statements:
if x < 6 or y > 4:
print(“A”)
elif x >= 8 and y < 6:
print(“B”)
if x > 10:
print(“C”)
else:
print(“D”)
1
if the following assignments are in effect:
a.
x = 9, y = 7
b.
x = 9, y = 2
c.
x = 6, y = 2
d.
x = 11, y = 3
Part 2: Programming
You have to implement two programs – numbersPrint.py and grades.py.
General guidelines for the two programs:

The first line in the program has to be a comment identifying you and the
assignment, e.g. # John Smith, cmsc 150, HW04

Next you have to write several lines as comments, describing what the program
does, based on the text of the problem.

Start the executable code with a welcome message saying briefly what the program
does
Write your name in front of the program name e.g. John_grades.py

Your work will be evaluated with respect to:

A running program, that meets the specified functional requirements (the dialogue
should look the way it is specified )

Quality of the comments in the source code: a comment should be relevant to the piece
of code it stands next to. It also should be meaningful.

Meaningful names of variables
Problem 1:
Write a program that prints the following:
12345
1234
123
12
1
2
Problem 2:
A professor needs a program that allows to enter the grades of a homework assignment and
store them in a tuple, find and print the highest grade, the lowest grade and the average. The
grades are real numbers – may have a decimal point.
Transcript of program run:
This program reads a sequence of grades and
finds the highest grade, the lowest grade, and the average grade
Please specify
Enter a grade:
Enter a grade:
Enter a grade:
Enter a grade:
Enter a grade:
how many grades you will enter? 5
83.5
90.6
92
72
80.8
Highest grade: 92.0
Lowest grade: 72.0
Average grade: 67.08
Guidelines:
For this program you will need to read the grades in a loop. Within the loop use tuple
concatenation to store the grades. Create an empty tuple grades outside the loop. Inside the
loop use a statement:
grades = grades + (currentGrade,)
note the presence of a comma after currentGrade, without it you'll get a run-time error.
Here is the algorithm:
(1) Read the number of grades to be entered
(2) create an empty tuple grades
(3) for i from 1 to number_of_grades (or from 0 to number_of_grades-1 do:
currentGrade ← user unput
append currentGrade to the existing tuple of grades
(4) find the highest grade and print it
(5) find the lowest grade and print it
(6) find the average and print it
Here is the algorithm to find the highest grade
(1) max ← grades[0]
(2) for i from 1 to number_of_grades-1 do
if grades[i] > max
max ← grades[i]
(3) print max
3
Download