How to use list in roulette game?

I've just started learning Python so apologies if I am missing anything obvious here.

I am trying to create a roulette game based off a question on Code Academy. I have decided to use a list rather than randomint because I want to add 00 to the list. That is the first thing I'm struggling with as I have used append to try and add 00 but it only adds it as 0.

The second thing I'm struggling with is fixing this error:

TypeError: unsupported operand type(s) for %: 'list' and 'int'

My code is below so it is the roulette_number part which is not working as it's come from a list and is therefore not being recognised as an int. Any suggestions would be much appreciated!

from random import seed
from random import sample

# seed random number generator

seed (1) # Seed is a reference to your random number so if you reference the same seed you will always get the same random number. Even having this randint doesn't make a difference. So in that case it doesn't matter what number you put in the brackets next to the seed?

# prepare a sequence
n = 1  
sequenceroulette = [i for i in range(37)] * n #The sequence is starting from 0 to 36
sequenceroulette.append(00) ## Try to work out how to add this as a double zero rather than one zero

print (sequenceroulette)

#select a subset without replacement

subset = sample(sequenceroulette,1) # we only want to pick 1 number a time from the list

roulette_number = subset

print (roulette_number)

money = 100

def roulette (Bet,Guess1, Guess2):
     if roulette_number % 2 == 0 and Guess1 == "Even" and Guess2 == "N/A":
         return (money - Bet) + (Bet * 2)
     elif roulette_number % 1 == 1 and Guess1 == "Odd" and Guess2 == "N/A":
         return (money - Bet) + (Bet * 2)
     elif roulette_number == Guess1 and Guess2 == "N/A":
         return (money - Bet) + (Bet *37) 
     elif roulette_number == Guess1 or roulette_number == Guess2:
         return (money - Bet) + (Bet * 18.5)
         return money

print (roulette(10,"Even","N/A"))

3 answers

  • answered 2020-02-12 23:08 Gabriel_D

    Replace this line:

    roulette_number = subset


    roulette_number = subset[0]

    Since subset if a list of one value you must get it to make the % operation

  • answered 2020-02-12 23:11 PacketLoss

    In Python, leading 0's are omitted when saving as an int. If you would like to store these, you need to save them as a string.


    In regards to your TypeError

    TypeError: unsupported operand type(s) for %: 'list' and 'int'

    You cannot use the operator % between an instance of list and integer.

    This is due to how you're pulling one number from the list with ;

    subset = sample(sequenceroulette,1)

    Notice, sample() still returns your result, in a list. If you update your roulette_number to always call element 0 you can resolve this error.

    roulette_number = subset[0]

    Hopefully this helps, if you want to do some more, indepth reading on some of the issues you encountered here are some links!



  • answered 2020-02-13 12:02 Sorath

    random.sample(population, k) 

    is a method that returns a list of length k.



    Will return a list with one number. In order to access this number in the list you need to do

    roulette_number = subset[0]

    When you are starting out with Python it's important to remember what data types the methods you are using return and therefore you need to use them in your code accordingly :)