+ 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.

11th Jul 2018, 12:58 PM
realcosmos
realcosmos - avatar
8 ответов
+ 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)
11th Jul 2018, 1:14 PM
Louis
Louis - avatar
+ 2
Thanks. This works too. having a hard time understanding this though. and the print fxn is not within the while variable.
12th Jul 2018, 8:43 AM
realcosmos
realcosmos - avatar
+ 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
11th Jul 2018, 1:08 PM
realcosmos
realcosmos - avatar
+ 1
Thanks so much.
11th Jul 2018, 1:50 PM
realcosmos
realcosmos - avatar
+ 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.
12th Jul 2018, 12:07 PM
Микола Федосєєв
Микола Федосєєв - avatar
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.
11th Jul 2018, 1:05 PM
Louis
Louis - avatar
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)
11th Jul 2018, 10:44 PM
Микола Федосєєв
Микола Федосєєв - avatar
0
Money outside play with man
3rd Mar 2024, 8:47 PM
Ro Chit Ro Chit (Rochit)
Ro Chit Ro Chit (Rochit) - avatar