Homework 7 Solutions
Solution Files
You can find the solutions in hw07.py.
Required Questions
Getting Started Videos
These videos may provide some helpful direction for tackling the coding problems on this assignment.
To see these videos, you should be logged into your berkeley.edu email.
Midsemester Survey
Q1: Mid-Semester Feedback
As part of this assignment, fill out the Mid-Semester Feedback form.
Once you finish the survey, you will be presented with a passphrase. Put
this passphrase, as a string, on the line that says
passphrase = 'REPLACE_THIS_WITH_PASSPHRASE'
in the Python file for this assignment.
E.g. if the passphrase is abc
, then the line should be passphrase = 'abc'
.
Use Ok to test your code:
python3 ok -q midsem_survey
Object-Oriented Programming
Q2: Vending Machine
In this question you'll create a vending machine that sells a single product and provides change when needed.
Implement the VendingMachine
class, which models a vending machine for one specific product.
The methods of a VendingMachine
object return strings to describe the machine’s status and operations.
Ensure that your output matches exactly with the strings provided in the doctests, including punctuation and spacing.
You may find Python's formatted string literals, or f-strings useful. A quick example:
>>> feeling = 'love' >>> course = '61A!' >>> combined_string = f'I {feeling} {course}' >>> combined_string 'I love 61A!'
class VendingMachine:
"""A vending machine that vends some product for some price.
>>> v = VendingMachine('candy', 10)
>>> v.vend()
'Nothing left to vend. Please restock.'
>>> v.add_funds(15)
'Nothing left to vend. Please restock. Here is your $15.'
>>> v.restock(2)
'Current candy stock: 2'
>>> v.vend()
'Please add $10 more funds.'
>>> v.add_funds(7)
'Current balance: $7'
>>> v.vend()
'Please add $3 more funds.'
>>> v.add_funds(5)
'Current balance: $12'
>>> v.vend()
'Here is your candy and $2 change.'
>>> v.add_funds(10)
'Current balance: $10'
>>> v.vend()
'Here is your candy.'
>>> v.add_funds(15)
'Nothing left to vend. Please restock. Here is your $15.'
>>> w = VendingMachine('soda', 2)
>>> w.restock(3)
'Current soda stock: 3'
>>> w.restock(3)
'Current soda stock: 6'
>>> w.add_funds(2)
'Current balance: $2'
>>> w.vend()
'Here is your soda.'
"""
def __init__(self, product, price):
"""Set the product and its price, as well as other instance attributes."""
self.product = product
self.price = price
self.stock = 0
self.balance = 0
def restock(self, n):
"""Add n to the stock and return a message about the updated stock level.
E.g., Current candy stock: 3
"""
self.stock += n
return f'Current {self.product} stock: {self.stock}'
def add_funds(self, n):
"""If the machine is out of stock, return a message informing the user to restock
(and return their n dollars).
E.g., Nothing left to vend. Please restock. Here is your $4.
Otherwise, add n to the balance and return a message about the updated balance.
E.g., Current balance: $4
"""
if self.stock == 0:
return f'Nothing left to vend. Please restock. Here is your ${n}.'
# Alternatively, we could have:
# return self.vend() + f' Here is your ${n}.'
self.balance += n
return f'Current balance: ${self.balance}'
def vend(self):
"""Dispense the product if there is sufficient stock and funds and
return a message. Update the stock and balance accordingly.
E.g., Here is your candy and $2 change.
If not, return a message suggesting how to correct the problem.
E.g., Nothing left to vend. Please restock.
Please add $3 more funds.
"""
if self.stock == 0:
return 'Nothing left to vend. Please restock.'
difference = self.price - self.balance
if difference > 0:
return f'Please add ${difference} more funds.'
message = f'Here is your {self.product}'
if difference != 0:
message += f' and ${-difference} change'
self.balance = 0
self.stock -= 1
return message + '.'
Use Ok to test your code:
python3 ok -q VendingMachine
Check Your Score Locally
You can locally check your score on each question of this assignment by running
python3 ok --score
This does NOT submit the assignment! When you are satisfied with your score, submit the assignment to Gradescope to receive credit for it.
Submit Assignment
Submit this assignment by uploading any files you've edited to the appropriate Gradescope assignment. Lab 00 has detailed instructions.
Exam Practice
These questions have no submission component; feel free to attempt them if you'd like some practice!
Object-Oriented Programming
- Spring 2022 MT2 Q8: CS61A Presents The Game of Hoop.
- Fall 2020 MT2 Q3: Sparse Lists
- Fall 2019 MT2 Q7: Version 2.0