DAPHNE // Python Tutor

← Back to Bridge
0 / 5 complete

Quick Reference

string concatstr1 + str2
int to strstr(42)
f-stringf"val={x}"
format $f"${x:,.2f}"
tuple sumsum(t)
tuple maxmax(t)
find indext.index(val)
tuple lenlen(t)
while loopwhile x < n:
powern ** 2
random choicer.choice(t)
precedenceand > or
1
Strings
40 pts
Produce the output:
Today, the temperature is 23.0 degrees Fahrenheit
  • Variable temperature = -5 (Celsius)
  • Variable start = "Today, the temperature is "
  • Variable end = " degrees Fahrenheit"
  • Convert using: F = (C × 9) / 5 + 32
  • Assemble everything into display using concatenation
# Strings temperature = # the temperature temp_F = # the formula start = # the beginning of the string end = # the end of the string display = # assemble the string here print(display)
variable assignment arithmetic operators str() conversion string concatenation (+)
💡 Hint 1 — What type is temp_F?
temp_F will be a float after the formula. You need str(temp_F) to concatenate it with strings.
💡 Hint 2 — Watch the math
(-5 * 9) / 5 + 32 = -9 + 32 = 23.0 — division with / always returns a float in Python.
💡 Hint 3 — Spaces matter
The spaces are baked into start and end. start ends with a space, end starts with a space. Just concatenate: start + str(temp_F) + end
▶ Show Solution
# Strings — Solution temperature = -5 temp_F = (temperature * 9) / 5 + 32 start = "Today, the temperature is " end = " degrees Fahrenheit" display = start + str(temp_F) + end print(display) # Output: Today, the temperature is 23.0 degrees Fahrenheit
2
Lists & Tuples
40 pts
Given:
month = ('Jan','Feb','Mar','Apr','May','June','July','Aug','Sep','Oct','Nov','Dec') expenses = (1800,2700,1202,3100,2100,1750,1845,2234,2141,3010,1911,2950)
Calculate and display:
Your total expenses are: $26,743 Your average expenses are: $2,228.58
  • Set budgetMax = 3000
  • Find highest monthly expense
  • If any expense exceeds budget, show:
You have exceeded your maximum budget at least once. The month with the highest expenses is April with $3100.
  • Change budgetMax = 3200 to get:
Congratulations! You kept your budget under control all year.
No loops allowed — use built-in functions only.
sum() len() max() .index() f-string formatting :,.2f format spec if/else
💡 Hint 1 — Formatting currency
Use f"${total:,}" for comma-separated integers.
Use f"${avg:,.2f}" for two decimal places with commas.
💡 Hint 2 — Finding the month name
max_exp = max(expenses) gives the highest value.
idx = expenses.index(max_exp) gives its position.
month[idx] gives the month name. But watch out — 'Apr' vs 'April'!
💡 Hint 3 — The budget check (no loops!)
You don't need to loop through expenses. Just check: if max(expenses) > budgetMax: — if the highest exceeds it, at least one did.
▶ Show Solution
# Lists and Tuples — Solution month = ('Jan','Feb','Mar','Apr','May','June','July','Aug','Sep','Oct','Nov','Dec') expenses = (1800,2700,1202,3100,2100,1750,1845,2234,2141,3010,1911,2950) # Step 1 total = sum(expenses) avg = total / len(expenses) print(f"Your total expenses are: ${total:,}") print(f"Your average expenses are: ${avg:,.2f}") # Step 2 budgetMax = 3000 max_exp = max(expenses) idx = expenses.index(max_exp) if max_exp > budgetMax: print("You have exceeded your maximum budget at least once.") print(f"The month with the highest expenses is {month[idx]} with ${max_exp}.") else: print("Congratulations! You kept your budget under control all year.") # Note: month[3] = 'Apr', but expected output says "April" # Your prof may want the full name — adjust the tuple or hardcode
3
While Loops
40 pts
Use a while loop to compute:
Sum from n=0 to 15 of n²
Validation: Sum from n=0 to 3 = 0 + 1 + 4 + 9 = 14
while condition accumulator pattern counter increment n ** 2
💡 Hint 1 — The pattern
You need two variables: a counter (n) starting at 0, and an accumulator (total) starting at 0. Each iteration: add n**2 to total, then increment n.
💡 Hint 2 — Loop boundary
while n <= 15: — use <= not < because we want to include n=15 itself.
💡 Hint 3 — Expected answer
The answer is 1240. (0+1+4+9+16+25+36+49+64+81+100+121+144+169+196+225)
▶ Show Solution
# While Loop — Solution n = 0 total = 0 while n <= 15: total += n ** 2 n += 1 print(total) # Output: 1240
4
Flow Control
40 pts
Given random selections from:
colors = ('green', 'red', 'white', 'yellow') vehicletype = ('priority', 'non-priority')
Build a message string that produces the correct output:
The light is green, you may proceed. The light is red, you must stop. The light is white, you must stop. (non-priority) The light is white, you may proceed as a priority vehicle. (priority) The light is yellow, you must prepare to stop.
Use random.choice() and if/elif/else to select the right action.
Graded on efficient use of if/elif/else — minimize nesting.
random.choice() if / elif / else nested conditions f-string assembly
💡 Hint 1 — Structure
First, pick a random color and vehicle type. Then use if/elif/else on the color. White is the only color that needs a nested check on vehicle type.
💡 Hint 2 — The action variables
action_1 = "you may proceed"
action_2 = "you must stop"
action_3 = "you must prepare to stop"
action_4 = "you may proceed as a priority vehicle"
Assign the right one to an action variable based on conditions.
💡 Hint 3 — Efficient branching
Only 4 branches needed:
if color == 'green': action = action_1
elif color == 'yellow': action = action_3
elif color == 'white' and vehicle == 'priority': action = action_4
else: action = action_2 # covers red AND white+non-priority
▶ Show Solution
# Flow Control — Solution import random as r colors = ('green', 'red', 'white', 'yellow') vehicletype = ('priority', 'non-priority') action_1 = 'you may proceed' action_2 = 'you must stop' action_3 = 'you must prepare to stop' action_4 = 'you may proceed as a priority vehicle' color = r.choice(colors) vehicle = r.choice(vehicletype) if color == 'green': action = action_1 elif color == 'yellow': action = action_3 elif color == 'white' and vehicle == 'priority': action = action_4 else: action = action_2 message = f"The light is {color}, {action}." print(message)
5
Boolean Tests
40 pts · paper
Evaluate each expression manually (no code). This section is pen-and-paper on the real exam.

Key rule: and has higher precedence than or. Evaluate and first, then or.
ExpressionYour AnswerCorrect
True or False Think first... True
False and True Think first... False
1 != 2 and ('a' == 'a' or 2 == 3) Think first... True
1 != 2 or 'a' == 'a' and 2 == 3 Think first... True
(1 < 2 or 3 == 3) and (1 == 0 or 1 != 1) Think first... False
(1 < 2 and 3 == 3) or (1 == 0 and 1 != 1) Think first... True
💡 r1: True or False
or returns True if either side is True. Left is True → True (short-circuits, never checks right side).
💡 r3: False and True
and returns False if either side is False. Left is False → False (short-circuits).
💡 r5: 1 != 2 and ('a' == 'a' or 2 == 3)
Step 1: 1 != 2 → True
Step 2: Inside parens: 'a' == 'a' → True, so (True or False) → True
Step 3: True and TrueTrue
💡 r7: 1 != 2 or 'a' == 'a' and 2 == 3 (tricky!)
No parentheses! and binds tighter than or.
Step 1: Evaluate the and first: 'a' == 'a' and 2 == 3True and False → False
Step 2: Now: 1 != 2 or FalseTrue or FalseTrue

This is the sneaky one — precedence makes it (1!=2) or (('a'=='a') and (2==3))
💡 r9: (1 < 2 or 3 == 3) and (1 == 0 or 1 != 1)
Step 1: Left group: 1 < 2 → True, so (True or True) → True
Step 2: Right group: 1 == 0 → False, 1 != 1 → False, so (False or False) → False
Step 3: True and FalseFalse
💡 r11: (1 < 2 and 3 == 3) or (1 == 0 and 1 != 1)
Step 1: Left group: True and True → True
Step 2: Right group: False and False → False
Step 3: True or FalseTrue