Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python 3.8 Question Below is a class Spinner: Complete the TODO in the following file: === Module Description === This module contains sample tests for

Python 3.8 Question

Below is a class Spinner:

image text in transcribed

image text in transcribed

Complete the TODO in the following file:

=== Module Description === This module contains sample tests for Prep 2. Complete the TODO in this file.

We suggest you also add your own tests to practice writing tests and to be confident your code is correct.

When writing a test case, make sure you create a new function, with its name starting with "test_". For example:

def test_my_test_case(): # Your test here """ from hypothesis import given from hypothesis.strategies import integers from prep2 import Spinner

def test_buggy_consecutive_spins() -> None: """Test consecutive spins of your Spinner class. This test case has a bug in it."""

# TODO: There is a bug in this test case -- find it and fix it. # In its original state, this test case should NOT pass if # your Spinner is implemented correctly! # Do NOT change or remove any of the lines labelled with # "Do not change this line".

s = Spinner(6) # Do not change this line s.spin(2) # Do not change this line expected_value1 = 2 assert s.position == expected_value1 # Do not change this line s.spin(2) # Do not change this line expected_value2 = 2 assert s.position == expected_value2 # Do not change this line

# === Sample test cases below === # Use the below test cases as an example for writing your own test cases, # and as a start to testing your prep2.py code.

# WARNING: THIS IS CURRENTLY AN EXTREMELY INCOMPLETE SET OF TESTS! # We will test your code on a much more thorough set of tests!

# For more information on hypothesis (one of the testing libraries we're using), # please see #

# This is a hypothesis test; it generates a random integer to use as input, # so that we don't need to hard-code a specific number of slots in the test. @given(slots=integers(min_value=1)) def test_new_spinner_position(slots: int) -> None: """Test that the position of a new spinner is always 0.""" spinner = Spinner(slots) assert spinner.position == 0

def test_doctest() -> None: """Test the given doctest in the Spinner class docstring.""" spinner = Spinner(8)

spinner.spin(4) assert spinner.position == 4

spinner.spin(2) assert spinner.position == 6

spinner.spin(2) assert spinner.position == 0

if __name__ == '__main__': import pytest pytest.main(['prep2_starter_tests.py'])

=== Module Description === This module contains the documentation for a simple class. Your job is to implement the class below according to its docstring; note this includes both the instance attributes* of the class and the *methods* we've documented. As usual, delete the TODO comments after you've completed each part. There is also a task inside prep2_starter_tests.py. Make sure to look at that file and complete the TODO there as well. # Do NOT use any imports beyond this one (randint)! from random import randint class Spinner: "A spinner for a board game. A spinner has a certain number of slots, numbered starting at 9 and increasing by 1 each slot. For example, if the spinner has they are numbered through 5, inclusive. A spinner also has an arrow that points to one of these slots. === Attributes === slots: The number of slots in this spinner. position: The slot number that the spinner's arrow is currently pointing to. === Sample Usage === Creating a spinner: >>> S = Spinner(8) >>> s. position Spinning the spinner: >>> s. spin(4) >>> s. position >>> s. spin(2) >>> s.position 6 >>> s. spin(2) >>> s.position slots: int position: int def __init__(self, size: int) -> None: "Initialize a new spinner with slots. A spinner's position always starts at 0. Precondition: slots >= 1 self.slots - [i for i in range(size)] self.position = 0 def spin(self, force: int) -> None: " Spin this spinner, advancing the arrow slots. The spinner wraps around once it reaches its maximum slot, starting back at 0. See the class docstring for an example of this. Precondition: force >= 0. Hint: use the "%" operator to "wrap around" the spinner's position. self.position += force self.position = self.position % len(self.slots) def spin_randomly(self) -> None: "Spin this spinner randomly. This modifies the spinner's arrow to point to a random slot on the spinner. Each slot has an equal chance of being pointed to. You MUST use randint (imported from random) for this method, to choose a random slot. self.spin(random.randint(0, len(self.slots)) if __name__ == '__main__': # When you run the module, you'll both run doctest and python_ta # to check your work. import doctest doctest.testmod() # python_ta opens up your web browser to display its report. # You want to see "None!" under both "Code Errors" and

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Constraint Databases

Authors: Peter Revesz

1st Edition

1441931554, 978-1441931559

More Books

Students also viewed these Databases questions