203k views
5 votes
We'd like to create a Keyboard class that takes in an arbitrary number of Buttons and stores these Buttons in a dictionary. The keys in the dictionary will be ints that represent the position on the Keyboard, and the values will be the respective Button. Fill out the methods in the Keyboard class according to each description, using the doctests as a reference for the behavior of a Keyboard.

1 Answer

4 votes

Answer:

class Button:

"""

Step-by-step explanation:

Represents a single button

"""

def __init__(self, pos, key):

"""

Creates a button

"""

self.pos = pos

self.key = key

self.times_pressed = 0

class Keyboard:

"""A Keyboard takes in an arbitrary amount of buttons, and has a

dictionary of positions as keys, and values as Buttons.

>>> b1 = Button(0, "H")

>>> b2 = Button(1, "I")

>>> k = Keyboard(b1, b2)

>>> k.buttons[0].key

'H'

>>> k.press(1)

'I'

>>> k.press(2) #No button at this position

''

>>> k.typing([0, 1])

'HI'

>>> k.typing([1, 0])

'IH'

>>> b1.times_pressed

2

>>> b2.times_pressed

3

"""

def __init__(self, *args):

________________

for _________ in ________________:

________________

def press(self, info):

"""Takes in a position of the button pressed, and

returns that button's output"""

if ____________________:

________________

________________

________________

________________

________________

def typing(self, typing_input):

"""Takes in a list of positions of buttons pressed, and

returns the total output"""

________________

for ________ in ____________________:

________________

________________

User Cee
by
7.9k points