Re : Creating dice game : NEED HELP ON CHECKING ELEMENTS IN A LIST

armand.foucault at telecom-bretagne.eu armand.foucault at telecom-bretagne.eu
Sat Oct 6 15:33:35 EDT 2018


   You could use any and a generator expression.
   It sounds pretty hard, but it's actually easily expressed, and pretty
   intelligible.
   Here you go:

   if any(n in result for n in [2, 3, 4, 6]):
   # reroll
   else:
   # do your thing

   ---------------------------------------------
   Armand FOUCAULT
   armand.foucault at telecom-bretagne.eu

   -------- Message original --------
   Objet**: Creating dice game : NEED HELP ON CHECKING ELEMENTS IN A LIST
   De**: eliteanarchyracer at gmail.com
   ****: python-list at python.org
   Cc**:

     Hi, I am new to python and would like someones help on the following:

     After rolling 5 dice and putting the results into a list, I need to
     check if any dice is not a 1 or 5.

     if all dice in the list are either a 1 or 5 , I want to calculate a
     result.
     if there is a 2,3,4 or 6 in the list i want to let the user roll again
     FOR EXACT LINE: PLEASE CHECK CODE

     CODE:

     from random import *

     class Dice:

     def __init__(self, sides, color="white"):
     self.sides = sides
     self.color = color

     def roll_dice(self):
     result = randint(1, self.sides)
     return result

     die1 = Dice(6)
     die2 = Dice(6)
     die3 = Dice(6)
     die4 = Dice(6)
     die5 = Dice(6)

     alldice = [die1, die2, die3, die4, die5]

     result = []

     start = input("Press enter to roll your dice:")

     for object in alldice:
     temp = object.roll_dice()
     result.append(temp)
     print(result)

     # ----- THIS LINE IS WHERE I NEED HELP ---- # ( if 2, 3, 4, 6 in list: )
     print("you can roll again")
     else:
     print("you have all 1's and 5's in your result")
     --
     https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list