# Python辅导 | CS 140 Homework 5

CS 140 Homework 5
Due October 25 at 11am (via Sakai Dropbox)
Usual late policy rules apply
Reminder about Collaboration Rules: (1) Students cannot share code with classmates or other that have taken CS
140. (2) If discussing the assignment with classmates, you cannot write any code in front of each other (3)
However, you can discuss general tactics and ways to approach problems. (4) PQRC mentors (including the TA)
have a specific protocol to follow when assisting you.
1. Write a program that selects a card at random from a standard deck of cards and prints out the suit
and value (on one line). Hint: There are four possible suits (heart, diamond, club, spade) and 13
possible values (2 – 9, Jack, Queen, King, Ace). Save your program as hw5p1.py
2. Write a python program that simulates tossing a pair dice by printing two random integers between
one and six. Save your program as hw5p2.py
3. Write a program to create some “Random Rectangle Art”.
 You must define variables for the width and height of the drawing window.
 You must allow the user to input the number of rectangles they want to draw.
 The rectangles should not be filled and must have a thickness of 2 pixels (recall that this is a
fourth argument in the pygame.draw.rect function very much like how you specify the
thickness when drawing a line).
 Use a while loop to draw rectangles. For each rectangle you must generate at random
 The color of the rectangle (recall that colors are ordered triples and each component
takes an integer value from 0 to 255)
 The x coordinate for the top left corner of the rectangle
 The y coordinate for the top left corner of the rectangle
 The width of the rectangle (this can be no larger than 1/10 the width of the window)
 The height of the rectangle (this can be no larger than 1/10 the height of the window)
4. Write a program that asks a user for an upper bound and uses a loop to identify all of the perfect
squares less than or equal to that upper bound. Save your program as hw5p4.py A few notes/hints:
 A perfect square is an integer that is the square of an integer. For example 121 is a perfect
square because 11*11 = 121.
 You will need to import the math module and use the math.sqrt( – ) function. This function
takes a single argument (the number you want to take the square root of). For example, to take
the square root of 4 you would type math.sqrt(4).
 Hint: to test if a number is a perfect square, you will need to use the number, the square root of
the number, and the modulus operator. Try this out first by typing in a few of the following
commands (in the Python Shell) until you recognize the general pattern.
1 % 1
4 % 2
9 % 3
16 % 4
 When grading this program, I will test it using some (large) undisclosed upper bound. Be sure
that your program is written general enough to accommodate something like this…
 A sample interaction with my program is provided below.
Please enter an upper bound: 10
I’m going to look for perfect squares less than or equal to 10
1 is a perfect square
4 is a perfect square
9 is a perfect square E-mail: vipdue@outlook.com  微信号:vipnxx 