studyguide2 - Second Homework Exam: Study Guide 1. Write a...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Second Homework Exam: Study Guide 1. Write a program that translates a number between 0 and 4 into the closest letter grade. For example the number 2.8 (which might have been the average of several grades) would be converted to B-. Break ties in favor of the better grade; for example 1.85 should be a C. There s more than one way to do this. grade = float(raw_input("Please enter grade: ")) if grade > 4: print "Number too big." elif grade >= 3.85: print "A" elif grade >= 3.5: print "A-" elif grade >= 3.15: print "B+" elif grade >= 2.85: print "B" elif grade >= 2.5: print "B-" elif grade >= 2.15: print "C+" elif grade >= 1.85: print "C" elif grade >= 1.5: print "C-" elif grade >= 1.15: print "D+" elif grade >= 0.85: print "D" elif grade >= 0.35: print "D-" elif grade >= 0: print "F" else: print "Number too small." Other ways would be to nest the if statements or to ask context-free questions. grade = float(raw_input("Please enter grade: ")) if grade > 4: print "Number too big." else: if grade >= 3.85: print "A" else: if grade >= 3.5: print "A-" else: if grade >= 3.15: print "B+" else: if grade >= 2.85: print "B" else: ... if grade > 4: print "Number too big." if 4 >= grade >= 3.85: print "A" if 3.85 > grade >= 3.5: print "A-" if 3.5 > grade >= 3.15: print "B+" if 3.15 > grade >= 2.85: print "B" ...
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2. Write a program that produces scalable patterns. The program asks the user for a size then prints a pattern of that size. Here are two examples: one program prints scalable Z s and the other one prints scalable 4 s. Other patterns are possible: A, T, E, L, F, R, Q. size = int(raw_input("Please enter the size: ")) for line in range(size): for column in range(size): if line + column == size - 1 or \ line == 0 or line == size-1 or \ line == size/2 and size/3 <= column <= 2 * size / 3: print "*", else: print " ", print >>> ================================ RESTART ================================ >>> Please enter the size: 13 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * >>> ================================ RESTART ================================ >>> Please enter the size: 18 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * >>> Note how long lines in Python can be split using a backslash for continuation. Next page has the second program that prints 4 s.
Background image of page 2
for line in range(size): for column in range(size): if line + column == size / 2 or \ line == size / 2 and column <= 3 * size / 4 or \ column == size / 2 and line >= size / 4: print "*", else: print " ", print >>> ================================ RESTART ================================ >>> Please enter the size: 9 * * * * * * * * * * * * * * * * * >>> ================================ RESTART ================================ >>> Please enter the size: 18 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * >>>
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/12/2011 for the course JAVA 2312 taught by Professor Darinbrezeale during the Spring '11 term at University of Texas at Austin.

Page1 / 9

studyguide2 - Second Homework Exam: Study Guide 1. Write a...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online