Question

Complete the methods from random import randint class Spinner: """A spinner for a board game. A...

Complete the methods
from random import randint


class Spinner:
    """A spinner for a board game.

    A spinner has a certain number of slots, numbered starting at 0 and
    increasing by 1 each slot. For example, if the spinner has 6 slots,
    they are numbered 0 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
    0

    Spinning the spinner:
    >>> s.spin(4)
    >>> s.position
    4
    >>> s.spin(2)
    >>> s.position
    6
    >>> s.spin(2)
    >>> s.position
    0
    """
    slots: int
    position: int

    def __init__(self, size: int) -> None:
        """Initialize a new spinner with <size> slots.

        A spinner's position always starts at 0.

        Precondition: size >= 1
        """
        # TODO: complete this method!
        pass

    def spin(self, force: int) -> None:
        """Spin this spinner, advancing the arrow <force> 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.
        """
        # TODO: complete this method!
        pass

    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.
        """
        # TODO: complete this method!
        pass


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
    # "Style and Convention Errors" for full marks.
    import python_ta
    python_ta.check_all(config={
        'extra-imports': ['random']
    })
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#### while pasting indentations/ tabs may get disturbed, please refer pic for correct indentations/tabs

import random
class Spinner:
def __init__(self, size):
if(size>=1):
self.slots=size
self.position=0

def spin(self, force):
self.position=(self.position+force)%self.slots
  

def spin_randomly(self):
self.position=random.randint(0,self.slots)

Add a comment
Know the answer?
Add Answer to:
Complete the methods from random import randint class Spinner: """A spinner for a board game. A...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • from __future__ import annotations from typing import Any, Optional class _Node: """A node in a linked...

    from __future__ import annotations from typing import Any, Optional class _Node: """A node in a linked list. Note that this is considered a "private class", one which is only meant to be used in this module by the LinkedList class, but not by client code. === Attributes === item: The data stored in this node. next: The next node in the list, or None if there are no more nodes. """ item: Any next: Optional[_Node] def __init__(self, item: Any) ->...

  • ill thumb up do your best python3 import random class CardDeck: class Card: def __init__(self, value):...

    ill thumb up do your best python3 import random class CardDeck: class Card: def __init__(self, value): self.value = value self.next = None def __repr__(self): return "{}".format(self.value) def __init__(self): self.top = None def shuffle(self): card_list = 4 * [x for x in range(2, 12)] + 12 * [10] random.shuffle(card_list) self.top = None for card in card_list: new_card = self.Card(card) new_card.next = self.top self.top = new_card def __repr__(self): curr = self.top out = "" card_list = [] while curr is not None:...

  • ***IN PYTHON 2.7****Augment the following code with a new class named 'Coin'. Coin should inherit from...

    ***IN PYTHON 2.7****Augment the following code with a new class named 'Coin'. Coin should inherit from Die, with the following modifications; The constructor should not take any arguments; a coin always has two sides add a flip() method that uses the roll() method from the parent class. If roll returns 1; flip should return "HEADS". If roll returns a 2, flip should return "TAILS" Do not override the roll or rollMultiple methods from the parent class #!/usr/bin/python # your class...

  • Player Class Represents a participant in the game of blackjack. Must meet the following requirements: Stores...

    Player Class Represents a participant in the game of blackjack. Must meet the following requirements: Stores the individual cards that are dealt (i.e. cannot just store the sum of the cards, you need to track which specific cards you receive) Provided methods: decide_hit(self): decides whether hit or stand by randomly selecting one of the two options. Parameters: None Returns: True to indicate a hit, False to indicate a stand Must implement the following methods: Hi!! i just need help with...

  • from __future__ import annotations import random from typing import TYPE_CHECKING, List, Any from course import sort_students...

    from __future__ import annotations import random from typing import TYPE_CHECKING, List, Any from course import sort_students if TYPE_CHECKING: from survey import Survey from course import Course, Student def slice_list(lst: List[Any], n: int) -> List[List[Any]]: """ Return a list containing slices of <lst> in order. Each slice is a list of size <n> containing the next <n> elements in <lst>. The last slice may contain fewer than <n> elements in order to make sure that the returned list contains all elements...

  • Task 2: SecretWord class: Download and save a copy of LinkedLists.py (found at the bottom of...

    Task 2: SecretWord class: Download and save a copy of LinkedLists.py (found at the bottom of this assignment page on eClass). In this file, you have been given the complete code for a LinkedList class. Familiarize yourself with this class, noticing that it uses the Node class from Task 1 and is almost identical to the SLinkedList class given in the lectures. However, it also has a complete insert(pos, item) method (which should be similar to the insert method you...

  • Copy the following Python fuction discussed in class into your file: from random import * def...

    Copy the following Python fuction discussed in class into your file: from random import * def makeRandomList(size, bound): a = [] for i in range(size): a.append(randint(0, bound)) return a a. Rename the function sumList as meanList and modify it so that it finds the average of the list. The average is the sum divided by the size (len) of the list. Make sure that the function doesn't give you an error when it is called on an empty list. The...

  • The following code skeleton contains a number of uncompleted methods. With a partner, work to complete...

    The following code skeleton contains a number of uncompleted methods. With a partner, work to complete the method implementations so that the main method runs correctly: /** * DESCRIPTION OF PROGRAM HERE * @author YOUR NAME HERE * @author PARTNER NAME HERE * @version DATE HERE * */ import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class ArrayExercises { /** * Given a random number generator and a length, create a new array of that * length and fill it from...

  • import math ''' Finish the code below as described. Use the completed class Square as an...

    import math ''' Finish the code below as described. Use the completed class Square as an example. ''' class Square: ''' Each Square has a width and can calculate its area, its perimeter, and return a string representation of itself. ''' def __init__(self, width): ''' (float) -> None Create a new Square with the given width. ''' self.width = width def get_area(self): ''' () -> float Return this square's area. ''' return self.width*self.width def get_perimeter(self): ''' () -> float Return...

  • import java.util.Arrays; import stdlib.*; public class CSC300Homework4 {    /**    * As a model for...

    import java.util.Arrays; import stdlib.*; public class CSC300Homework4 {    /**    * As a model for Problem 1, here are two functions to find the minimum value of an array of ints    * an iterative version and a recursive version    *    * precondition: list is not empty    /** iterative version */    public static double minValueIterative (int[] list) {        int result = list[0];        int i = 1;        while (i <...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT