0

Python core practice 37.2 "Modules"

Two friends want to play backgammon, but have lost the dice. Create a program to replace the dice. When the program is run, it should roll the dice and output the result of each die. Hint Use random.randint() function to generate random values in the range of 1 to 6 for each dice. You will see a random.seed(int(input())) line in sample code. It initializes the pseudorandom number generator and, in this case, ensures functionality of test cases. My code: import random random.seed(int(input())) #please don't touch this lane #generate the random values for every dice dice1 = random.randint(1, 7) dice2 = random.randint(1, 7) print(dice1) print(dice2) This code fails hidden test 2 The code that you will see below passes all tests. import random random.seed(int(input())) #please don't touch this lane dice1 = random.randint(1, 6) dice2 = random.randint(1, 6) print(dice1) print(dice2) If you write 5 instead of 6, the test will also pass. Someone can tell why this is happening, because "6" should not be accepted. Thanks

24th Aug 2021, 12:49 PM
Tsimur Kuts
Tsimur Kuts - avatar
4 ответов
+ 3
The function randint() includes both numbers into its range, so that randint(1, 7) includes 7, making it fail th test. But it may simply be the malfunction of the test cases so that randint(1, 5) passed…
24th Aug 2021, 12:57 PM
Wenkai Qu
Wenkai Qu - avatar
+ 1
Oh thank you. For some reason I thought that ALWAYS the second number is not included
24th Aug 2021, 12:59 PM
Tsimur Kuts
Tsimur Kuts - avatar
0
Tsimur Kuts Very welcome~
24th Aug 2021, 1:00 PM
Wenkai Qu
Wenkai Qu - avatar
0
#why then this option does not work, it also outputs random 2 numbers import random for i in range(2): print( random.randint(1,7))
13th Sep 2021, 6:18 PM
Tedi
Tedi - avatar