Homework

1.3.3. Homework#

  • Use these questions after class to check your understanding of the chapter concepts and coding skills.

  • Sign in from the account menu before you start working on the homework.

  • Complete all true/false and coding questions before you submit the homework.

  • Launch Live Code (the rocket) to write and run code in the coding cells.

  • Multiple submissions are allowed. The highest score is kept.

1. Programs

A manager asks an analyst to write a repeatable set of Python instructions that prepares the weekly sales report. Calling that set of instructions a program is appropriate.

Hide code cell source

### Solution
# True
# A repeatable set of Python instructions that prepares a report is a program.

2. Algorithms

Before assigning a dashboard task, a team lead sketches the step-by-step logic for cleaning, grouping, and summarizing customer records without choosing Python syntax yet. That sketch can still be an algorithm.

Hide code cell source

### Solution
# True
# An algorithm can describe the step-by-step logic before the team chooses exact Python syntax.

3. Syntax

A supervisor reviews a script that has the right business logic but missing parentheses and mismatched quotes. Because the business logic is correct, Python syntax rules do not matter for whether the script can run.

Hide code cell source

### Solution
# False
# Correct business logic is not enough; syntax errors can stop Python before the script runs.

4. Comments

A project manager asks the team to add notes explaining why a pricing rule exists. If those notes are written as Python lines starting with #, they normally document the script without changing what runs.

Hide code cell source

### Solution
# True
# Lines that start with # are comments, so they document the script without changing execution.

5. Input Values

An operations form asks for the number of units to reorder. If a Python script reads that value with input(), Python automatically treats it as a number whenever the next line multiplies it by a price.

Hide code cell source

### Solution
# False
# input() returns text, so numeric form values usually need conversion before arithmetic.
### 6. Fix a Syntax Error
# The code below contains one syntax error, a missing closing parenthesis:
# print("ThinkPy homework"
# print("Chapter 1 is about first programs")
# Write the corrected version so it prints exactly:
# ThinkPy homework
# Chapter 1 is about first programs
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
print("ThinkPy homework")
print("Chapter 1 is about first programs")
ThinkPy homework
Chapter 1 is about first programs
### 7. Update a Running Count
# The lab asked you to calculate values once. Here, update a variable as a count changes.
# Start with open_tickets. Add new_tickets, subtract closed_tickets, and print exactly:
# Open tickets: 9

open_tickets = 12
new_tickets = 5
closed_tickets = 8

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
open_tickets = 12
new_tickets = 5
closed_tickets = 8

open_tickets = open_tickets + new_tickets - closed_tickets

print("Open tickets:", open_tickets)
Open tickets: 9
### 8. Inspect Converted Input
# The lab converted text input to a number. Here, confirm the type changes after conversion.
# Convert quantity_text to an integer named quantity.
# Print exactly:
# Before: <class 'str'>
# After: <class 'int'>

quantity_text = "42"

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
quantity_text = "42"
quantity = int(quantity_text)

print("Before:", type(quantity_text))
print("After:", type(quantity))
Before: <class 'str'>
After: <class 'int'>
### 9. Write a Stakeholder Message
# The lab practiced formatted output. Here, write a short message for a business reader.
# Use an f-string and the variables below to print exactly:
# North region needs 6 follow-up calls.

region = "North"
follow_up_calls = 6

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
region = "North"
follow_up_calls = 6

print(f"{region} region needs {follow_up_calls} follow-up calls.")
North region needs 6 follow-up calls.
### 10. Build an Audit Tag
# The lab printed separate number-system and character-code values.
# Here, combine an initial, its numeric code, and a hex item number into one tag.
# Use ord(), str(), and hex() to print exactly:
# Audit tag: A-65-0xff

initial = "A"
item_number = 255

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
initial = "A"
item_number = 255

initial_code = ord(initial)
item_hex = hex(item_number)
tag = initial + "-" + str(initial_code) + "-" + item_hex

print("Audit tag:", tag)
Audit tag: A-65-0xff