+ 1
Dice Game
# NOW trying to write a code such that diceA is never equal to (!=) diceB i wrote this import random diceA = random.randint(1,6) diceB= random.randint(1,6) while True: if diceA != diceB : Roll_a=str(diceA+diceB) print ("Result:"+Roll_a) break else: diceB2 = random.randint(1,6) Roll_b=str(diceA+diceB2) print("Result:"+ Roll_b) break #output still returned 12 after sometime.
8 Answers
+ 2
#Try this
from random import choice as c
dice_out=list(range(1,7))
diceA = c(dice_out)
diceB= c([i for i in dice_out if i!= diceA])
print(diceA+diceB)
+ 2
Thanks.
This works too.
having a hard time understanding this though.
and the print fxn is not within the while variable.
+ 1
I'm a newbie. I saw that too. but then I pressed f5 so many times before that logic failed.
how do I limit diceB
+ 1
Thanks so much.
+ 1
In general, this means
try
while "not OK"
try again
some other languages allow post loop verification, so it looks like
do
try
while "not OK"
this is easier to read.
0
There is an error in your logic.
In the case where else: is called, nothing limmits diceB2 from ending up the same as diceA.
0
#output still returned 12 after sometime.
this happens when all diceA, diceB and diceB2 equal 6.
try the following:
import random
diceA = random.randint(1,6)
diceB = random.randint(1,6)
while diceB == diceA :
diceB = random.randint(1,6)
print("Result:", diceA + diceB)
0
Money outside play with man