活跃农民
- 积分
- 633
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2012-4-1
- 最后登录
- 1970-1-1
|
第四题真心不会做,startwith是什么鬼 ,其余的都做出来了,代码如下:
#hw05
#Q1
def make_withdraw(balance, password):
"""Return a password-protected withdraw function.
>>> w = make_withdraw(100, 'hax0r')
>>> w(25, 'hax0r')
75
>>> w(90, 'hax0r')
'Insufficient funds'
>>> w(25, 'hwat')
'Incorrect password'
>>> w(25, 'hax0r')
50
>>> w(75, 'a')
'Incorrect password'
>>> w(10, 'hax0r')
40
>>> w(20, 'n00b')
'Incorrect password'
>>> w(10, 'hax0r')
"Your account is locked. Attempts: ['hwat', 'a', 'n00b']"
>>> w(10, 'l33t')
"Your account is locked. Attempts: ['hwat', 'a', 'n00b']"
"""
"*** YOUR CODE HERE ***"
l = [password]
password_wrong =[]
def withdraw(amount,password_att):
nonlocal balance
# nonlocal password
if len(password_wrong) < 3:
if password_att in l:
if amount > balance:
return 'Insufficient funds'
balance = balance - amount
return balance
if password_att not in l:
# if len(password_wrong) < 3:
password_wrong.append(password_att)
print ("'Incorrect password'")
else:
print ("Your account is locked. Attempts: " + '[' + str(password_wrong[0]) +
', ' + password_wrong[1] + ', ' + password_wrong[2] + ']')
return withdraw
#Q2
def make_joint(withdraw, old_password, new_password):
"""Return a password-protected withdraw function that has joint access to
the balance of withdraw.
>>> w = make_withdraw(100, 'hax0r')
>>> w(25, 'hax0r')
75
>>> make_joint(w, 'my', 'secret')
'Incorrect password'
>>> j = make_joint(w, 'hax0r', 'secret')
>>> w(25, 'secret')
'Incorrect password'
>>> j(25, 'secret')
50
>>> j(25, 'hax0r')
25
>>> j(100, 'secret')
'Insufficient funds'
>>> j2 = make_joint(j, 'secret', 'code')
>>> j2(5, 'code')
20
>>> j2(5, 'secret')
15
>>> j2(5, 'hax0r')
10
>>> j2(25, 'password')
'Incorrect password'
>>> j2(5, 'secret')
"Your account is locked. Attempts: ['my', 'secret', 'password']"
>>> j(5, 'secret')
"Your account is locked. Attempts: ['my', 'secret', 'password']"
>>> w(5, 'hax0r')
"Your account is locked. Attempts: ['my', 'secret', 'password']"
>>> make_joint(w, 'hax0r', 'hello')
"Your account is locked. Attempts: ['my', 'secret', 'password']"
"""
"*** YOUR CODE HERE ***"
if type(withdraw(0, old_password)) == str:
return withdraw(0, old_password)
def join_account(amount, password):
if password == new_password:
password = old_password
return withdraw(amount, password)
return join_account
#Q3
class VendingMachine:
"""A vending machine that vends some product for some price.
>>> v = VendingMachine('candy', 10)
>>> v.vend()
'Machine is out of stock.'
>>> v.restock(2)
'Current candy stock: 2'
>>> v.vend()
'You must deposit $10 more.'
>>> v.deposit(7)
'Current balance: $7'
>>> v.vend()
'You must deposit $3 more.'
>>> v.deposit(5)
'Current balance: $12'
>>> v.vend()
'Here is your candy and $2 change.'
>>> v.deposit(10)
'Current balance: $10'
>>> v.vend()
'Here is your candy.'
>>> v.deposit(15)
'Machine is out of stock. Here is your $15.'
"""
"*** YOUR CODE HERE ***"
def __init__(self, product_name, price):
self.stock = 0
self.name = product_name
self.price = price
self.balance = 0
def vend(self):
if self.stock == 0:
return 'Machine is out of stock.'
if self.stock > 0:
if self.balance < self.price:
print("You must deposit $" + str(self.price - self.balance) + " more.")
if self.balance > self.price:
print("Here is your " + str(self.name) + " and $"
+ str(self.balance - self.price) + " change.")
self.balance = 0
self.stock -= 1
if self.balance == self.price:
self.balance = 0
print("Here is your " + str(self.name) + '.')
self.stock -= 1
def restock(self, x):
self.stock += x
print("Current " + str(self.name) + " stock: " + str(self.stock))
def deposit(self, x):
if self.stock == 0:
print("Machine is out of stock. Here is your $" + str(x))
else:
self.balance += x
print("Current balance: $" + str(self.balance))
#Q4
class MissManners:
"""A container class that only forward messages that say please.
>>> v = VendingMachine('teaspoon', 10)
>>> v.restock(2)
'Current teaspoon stock: 2'
>>> m = MissManners(v)
>>> m.ask('vend')
'You must learn to say please first.'
>>> m.ask('please vend')
'You must deposit $10 more.'
>>> m.ask('please deposit', 20)
'Current balance: $20'
>>> m.ask('now will you vend?')
'You must learn to say please first.'
>>> m.ask('please hand over a teaspoon')
'Thanks for asking, but I know not how to hand over a teaspoon'
>>> m.ask('please vend')
'Here is your teaspoon and $10 change.'
>>> really_fussy = MissManners(m)
>>> really_fussy.ask('deposit', 10)
'You must learn to say please first.'
>>> really_fussy.ask('please deposit', 10)
'Thanks for asking, but I know not how to deposit'
>>> really_fussy.ask('please please deposit', 10)
'Thanks for asking, but I know not how to please deposit'
>>> really_fussy.ask('please ask', 'please deposit', 10)
'Current balance: $10'
>>> fussy_three = MissManners(3)
>>> fussy_three.ask('add', 4)
'You must learn to say please first.'
>>> fussy_three.ask('please add', 4)
'Thanks for asking, but I know not how to add'
>>> fussy_three.ask('please __add__', 4)
7
"""
"*** YOUR CODE HERE ***"
def __init__(self, object):
self.object = object
def ask(self, message, *args):
magic_word = 'please '
if not message.startswith(magic_word):
return 'You must learn to say please first.'
attr = message[len(magic_word):]
if not hasattr(self.object, attr):
return 'Thanks for asking, but I know not how to ' + attr
return getattr(self.object, attr)(*args) |
|