Skip to content

Instantly share code, notes, and snippets.

@SteveDaulton
Created November 16, 2023 11:52
Show Gist options
  • Save SteveDaulton/00a1701cf509926651dce1720b478502 to your computer and use it in GitHub Desktop.
Save SteveDaulton/00a1701cf509926651dce1720b478502 to your computer and use it in GitHub Desktop.
Roulette Wheel simulation
"""Roulette wheel simulation."""
from random import randrange
def wheel_spin() -> int:
"""Return result of roulette wheel spin.
Returns
-------
tuple
The winning number and its colour.
"""
number = randrange(37)
return (number, get_color(number))
def get_color(number: int) -> str:
"""Return the colour of the roulette wheel cell.
Wheel numbers are in a specified order, and alternate
between red and black, excluding the number 0 which is
green.
Returns
-------
str
The colour associated with number.
Raises
------
ValueError
When number is out of range.
"""
# Layout of numbers on a "single zero" wheel,
# starting with "red 32" and excluding zero.
non_zero_wheel_numbers = (
32, 15, 19, 4, 21, 2, 25, 17, 34, 6, 27, 13, 36,
11, 30, 8, 23, 10, 5, 24, 16, 33, 1, 20, 14,
31, 9, 22, 18, 29, 7, 28, 12, 35, 3, 26)
colors = ("red", "black")
if number == 0:
return "green"
try:
index = non_zero_wheel_numbers.index(number)
except ValueError as exc:
raise ValueError (f"{number} is out of range. {exc}")
return colors[index % 2]
# Test it.
for _ in range(10):
result = wheel_spin()
print(f"Number: {result[0]} colour: {result[1]}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment