Practice Quiz 2 (Ch 3–4)¶
Name: ___________________________________
This work complies with the JMU Honor Code.
I have neither given nor received unauthorized assistance.
I will not discuss the quiz contents with anyone who has not taken the quiz.
Initials: ____________
Written Portion¶
Question 1 (3 pts)¶
Based on the code snippet, circle the best example for each term.
def can_vote(age, registered):
if registered:
if age <= 0:
message = "Input Error: Age is invalid!"
elif age < 18:
message = "You have to be at least 18 to vote!"
else:
message = "You are eligible to vote!"
else:
message = "You are not registered to vote!"
return message
print(can_vote(0, True))
Argument
- age
- can_vote
- message
- True ✓
Function Call
- can_vote(0, True) ✓
- def can_vote(age, registered)
- message = "You are eligible to vote!"
- return message
Local Variable
- age
- can_vote
- message ✓
- registered
Boolean Literal
- age < 18
- registered
- True ✓
- 0
Function Definition
- can_vote(0, True)
- def can_vote(age, registered) ✓
- message = "You are eligible to vote!"
- return message
Parameter
- age ✓
- message
- 0
Question 2 (4 pts)¶
def first(a, b):
second(b, a)
print(f"First: {a} {b}", end=" ")
def second(x, y):
print(f"Second: {x} {y}", end=" ")
a.
What will be printed when second()
is called as follows?
x = "car"
y = "tree"
second(y, x) # Note the argument order!
Second: tree car
b.
What will be printed when first()
is called as follows?
x = "car"
y = "tree"
first(x, y)
Second: tree car First: car tree
Question 3 (3 pts)¶
Evaluate the logic expressions below.
Expression | Result (True, False, or Error) |
---|---|
10 + 4 >= 11 |
True |
False or True and False |
False |
not 4 > 2 or 5.4 < 4.5 |
False |
True and False or 5 = 4 |
Error |
3 < 5 and 3 <= 3 |
True |
"CS149"[4] == 9 |
False |
Coding Portion¶
Note
On quizzes, you are required to write a docstring only at the top of the file:
"""Practice Quiz 2.
Author: Your Name
Version: The Date
"""
Write the two functions described below in a single file named practice_quiz2.py
.
percent_to_float (5 pts)¶
"""Take the int percentage passed in and return it as the equivalent float. Examples: percent_to_float(95) returns 0.95 percent_to_float(50) returns 0.5 Args: percent (int): the percentage to convert Returns: float: the percent as a float """
donut_fundraiser (10 pts)¶
"""Calculate the amount of money a club raises by selling donuts. - The club gets $2 per box of donuts sold. - If they sell more than 50 boxes, they get a special rate of $3/box on all boxes sold. - If they sell more than 100 boxes, then the donut company will give the club $200 in addition to the money they make per box. - If the club has held more than 3 donut fundraisers in the past and sells over 100 boxes, the donut company will give the club an additional $300 bonus. Examples: donut_fundraiser(30, 4) returns 60 ($2/box) donut_fundraiser(60, 0) returns 180 ($3/box) donut_fundraiser(110, 3) returns 530 ($3/box + $200 bonus) donut_fundraiser(110, 4) returns 830 ($3/box + $200 bonus + $300 bonus) Args: boxes (int): the number of boxes sold times (int): the number of times the club has done a donut fundraiser Returns: int: the amount of money raised for the club """