Problem 1: Vending Machine (100pts)
Problem
Create a class called
VendingMachinethat represents a vending machine for some product. AVendingMachineobject returns strings describing its interactions. Fill in theVendingMachineclass, adding attributes and methods as appropriate, such that its behavior matches the following doctests:
创建一个名为 VendingMachine 的类,用于表示某种产品的自动售货机。VendingMachine 对象返回描述其交互的字符串。
填充 VendingMachine 类,添加适当的属性和方法,使其行为与以下 doctests 匹配:
class VendingMachine:
"""A vending machine that vends some product for some price.
>>> v = VendingMachine('candy', 10)
>>> v.vend()
'Machine is out of stock.'
>>> v.add_funds(15)
'Machine is out of stock. Here is your $15.'
>>> v.restock(2)
'Current candy stock: 2'
>>> v.vend()
'You must add $10 more funds.'
>>> v.add_funds(7)
'Current balance: $7'
>>> v.vend()
'You must 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)
'Machine is out of stock. 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: str, price: int):
"""Set the product and its price, as well as other instance attributes."""
"*** YOUR CODE HERE ***"
def restock(self, quantity: int) -> str:
"""Add quantity to the stock and return a message about the updated stock level.
E.g., Current candy stock: 3
"""
assert quantity > 0
"*** YOUR CODE HERE ***"
def add_funds(self, funds: int) -> str:
"""If the machine is out of stock, return a message informing the user to restock
(and return their n dollars).
E.g., Machine is out of stock. Here is your $4.
Otherwise, add funds to the balance and return a message about the updated balance.
E.g., Current balance: $4
"""
"*** YOUR CODE HERE ***"
def vend(self) -> str:
"""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.
E.g., Here is your candy and $2 change.
If not, return a message suggesting how to correct the problem.
E.g., Machine is out of stock.
E.g., You must add $3 more funds.
"""
"*** YOUR CODE HERE ***"
Hints
Hint1: You may find Python string formatting syntax or f-strings useful. A quick example:
>>> ten, twenty, thirty = 10, 'twenty', [30] >>> '{0} plus {1} is {2}'.format(ten, twenty, thirty) '10 plus twenty is [30]' >>> feeling = 'love' >>> course = 'SICP' >>> year = 2025 >>> f'I {feeling} {course}-{year}!' 'I love SICP-2025!'Hint2: Make sure to handle corner cases, such as trying to vend when the machine is out of stock.
Hint3: For the ease of understanding, we provide type hints for each parameter in every method. For example,
quantity: intandproduct: strindicate thatquantitywill be an integer, whileproductwill be a string. For functions (or methods),def vend(self) -> strindicates thatvend()should return a string. You can assume that all inputs to the methods will be of the correct type. You do not need to handle invalid input types.
- 您可能会发现 Python 字符串格式化语法 或 f-strings 很有用。一个简单的例子:
>>> ten, twenty, thirty = 10, 'twenty', [30]
>>> '{0} plus {1} is {2}'.format(ten, twenty, thirty)
'10 plus twenty is [30]'
>>> feeling = 'love'
>>> course = 'SICP'
>>> year = 2025
>>> f'I {feeling} {course}-{year}!'
'I love SICP-2025!'
-
确保处理边界情况,例如机器缺货时尝试售卖。
-
为了方便理解,我们为每个方法中的每个参数提供了 类型提示。 例如,
quantity: int和product: str表示quantity将是一个整数,而product将是一个字符串。 对于函数(或方法),def vend(self) -> str表示vend()应该返回一个字符串。 您可以假设输入给方法的所有输入都将是正确的类型。您不需要处理无效的输入类型。