Question: Hello, I'm a beginner CS student learning object oriented programming. I've been tasked with coding a Keyboard class, and I started some code on it,
Hello, I'm a beginner CS student learning object oriented programming. I've been tasked with coding a "Keyboard" class, and I started some code on it, but it isn't working, and I can't figure out why. Any help is greatly appreciated. Thanks!
"We'd like to construct aKeyboardclass that takes in an arbitrary number ofButtons and stores theseButtons in a dictionary. The keys in the dictionary will be ints that represent the position on theKeyboard, and the values will be the respectiveButton. Fill out the methods in theKeyboardclass according to each description, using the doctests as a reference for the behavior of aKeyboard."
class Keyboard: """A Keyboard takes in a list of buttons, and has a dictionary of positions as keys, and Buttons as values. >>> b1 = Button("button1", "H") >>> b2 = Button("button2", "I") >>> k = Keyboard([b1, b2]) >>> "button1" in k.buttons.keys() # Make sure to add the button to dictionary True >>> k.buttons["button1"].letter 'H' >>> k.buttons["button1"].name 'button1' >>> k.press("button1") 'H' >>> k.press("button100") '' >>> b1.pressed 1 >>> b2.pressed 0 >>> k.typing(["button1", "button2"]) 'HI' >>> k.typing(["button2", "button1"]) 'IH' >>> b1.pressed # make sure typing calls press! 3 >>> b2.pressed 2 """ def __init__(self, buttons): "*** YOUR CODE HERE ***" self.buttons = {} for button in buttons: self.buttons[button] = button def press(self, name): """Takes in a position of the button pressed, and returns that button's output. Return an empty string if the button does not exist. You can access the keys of a dictionary d with d.keys(). """ "*** YOUR CODE HERE ***" if name in self.buttons: b = self.buttons[name] buttons += name return b return '' def typing(self, typing_input): """Takes in a list of buttons to be pressed, and returns the total output. Make sure to call self.press""" "*** YOUR CODE HERE ***" output = '' for input in typing_input: output += self.press(input) return output class Button: def __init__(self, name, letter): self.name = name self.letter = letter self.pressed = 0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
