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

2018-10-10 Thread Abdur-Rahmaan Janhangeer
*if any(roll != 1 and roll != 5 for roll in result):*

another extract of py's beauty!

Abdur-Rahmaan Janhangeer
https://github.com/Abdur-rahmaanJ
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2018-10-10 Thread Neil Cerutti
On 2018-10-06, eliteanarchyra...@gmail.com
 wrote:
> 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 any(roll != 1 and roll != 5 for roll in 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")

Ha! Didn't think I'd get to apply DeMorgan's Law so soon.

-- 
Neil Cerutti
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2018-10-06 Thread bob gailer
# your program is quite complicated. classes are overkill. A simpler 
solution is:

import random
for i in range(5):
    roll = random.randint(1,6)
    if roll not in (1,5):
    print('you can roll again')
    break
else:
    print("you have all 1's and 5's in your result'")

# comments on  using classes:
class Dice:
    ...
# no need for die1...die5 - use list comprehension:
alldice = [Dice(6) for i in range(5)]

# object is a bult-in type. it is inadvisable to assign to names of 
built-ins
# as this makes the built-in unavailable. Look up builtins (module) in 
help for

# a complete list.

# use list comprehension roll and report:
print([d.roll_dice() for d in alldice])

--
https://mail.python.org/mailman/listinfo/python-list


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

2018-10-06 Thread eliteanarchyracer
Well these worked like a charm. Cant say I understand some of them and will 
have to look into utilizing them in the future, but I appreciate everyones 
responses and thank you all. Never needed to check for multiple instances in a 
list before.

Nice to have help!
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2018-10-06 Thread Alister via Python-list
On Sat, 06 Oct 2018 21:56:09 +0200, Thomas Jollans wrote:

> On 06/10/2018 20:10, eliteanarchyra...@gmail.com wrote:
>> # - 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")
>> 
>> 
> You can use a loop:
> 
> for good_number in [2,3,4,6]:
>  if good_number in result:
>  print("you can roll again")
>  break
> else:
>  print("you have all 1's and 5's in your result")

or simply check for 1 & 5 to exit otherwise continue



-- 
Someone hooked the twisted pair wires into the answering machine.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2018-10-06 Thread armand.fouca...@telecom-bretagne.eu
   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.fouca...@telecom-bretagne.eu

    Message original 
   Objet**: Creating dice game : NEED HELP ON CHECKING ELEMENTS IN A LIST
   De**: eliteanarchyra...@gmail.com
   : python-list@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
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2018-10-06 Thread Thomas Jollans

On 06/10/2018 20:10, eliteanarchyra...@gmail.com wrote:

# - 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")



You can use a loop:

for good_number in [2,3,4,6]:
if good_number in result:
print("you can roll again")
break
else:
print("you have all 1's and 5's in your result")
--
https://mail.python.org/mailman/listinfo/python-list


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

2018-10-06 Thread MRAB

On 2018-10-06 19:10, eliteanarchyra...@gmail.com wrote:

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")


The simplest (and longest) way is:

if 2 in result or 3 in result or 4 in result or 6 in result:

A cleverer way is to use sets:

if set(result) & {2, 3, 4, 6}:
--
https://mail.python.org/mailman/listinfo/python-list