1

im trying to make it so that it will do the calculation no matter what the number inputted is, im not very experienced in python and im making something that will have a dice with 'x' amount of sides and it will look for 'x' amount of pairs to see for example how many dice rolls it would take to roll 6 sixes. sorry if ive worded this badly

time is how many times its rolled the dice
pairs is the amount of pairs it is looking for
sides is how many sides/numbers the dice has

import random

sides = int(input("How many Dice Sides/Numbers: "))
pairs = int(input("How many Pairs to Search for: "))

time = 0

if pairs == 2:
    while True:
        time += 1
        one = random.randint(1, sides)
        two = random.randint(1, sides)
        if one == two:
            break
if pairs == 3:
    while True:
        time += 1
        one = random.randint(1, sides)
        two = random.randint(1, sides)
        three = random.randint(1, sides)
        if one == two and two == three:
            break

input(f"Found a Pair after {time} times.")

result of above code

i.e how would i make it work with ANY number i input for the pairs, sorry again if this is not possible

ikyyntts
  • 27
  • 2
  • 1
    Welcome to SO. Please [edit] and paste any code/errors/output as text. See: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/a/285557) – 001 May 14 '21 at 17:05
  • The image isn't representive of the code. There's not one `print`. Make a [mcve]. – Mark Tolonen May 14 '21 at 17:12
  • @MarkTolonen would you mean like the working piece of code? like the full one ive got now? – ikyyntts May 14 '21 at 17:14
  • Yes, the working script, reduced to just focus on the problem if possible. Describe actual vs. desired behavior. Read the [mcve] link. – Mark Tolonen May 14 '21 at 17:18
  • @ikyyntts - that's not a full working example. For a question like this we should be able to copy/paste and then run it. – tdelaney May 14 '21 at 17:19
  • @MarkTolonen i changed it to the full code – ikyyntts May 14 '21 at 17:21
  • 2
    @tdelaney i added the full code – ikyyntts May 14 '21 at 17:21
  • @JonSG im a little confused, i wanted my code to see how many attempts it would take for it to be able to have all rolls matching (all the same rolls) on an any amount sided dice. my roadblock was making it work for finding any amount of pairs as it would take an unbearable amount of time to do manually – ikyyntts May 14 '21 at 17:45

3 Answers3

1

You're on the right track, but it's just a matter of simplifying your code, substituting the quantity of variables for variables which are more 'efficient' (in this case, that would be using data structures, such as lists). You can use list comprehension to generate a list of random numbers with variable length:

[random.randint(lower, upper) for _ in range(n)]

This will generate n random numbers between lower and upper, inclusive.


To check if all the numbers are equal, one method would be by checking that the number of times the first item in the list occurs within the list is equal to the length of the list itself. (You can find a variety of alternative methods in this Stack Overflow answer.)

len(lst) == lst.count(lst[0])

Putting that all together, you would have a function similar to this:

def find_pairs(sides, pairs):
    rolls = 0
    while True:
        rolls += 1
        nums = [random.randint(1, sides) for _ in range(pairs)]
        if len(nums) == nums.count(nums[0]):
            break
    return rolls

Some sample runs:

>>> find_pairs(1, 6)
1
>>> find_pairs(2, 6)
28
>>> find_pairs(3, 6)
79
Jacob Lee
  • 4,405
  • 2
  • 16
  • 37
1

When you want to work with a variable number of things, a list or dict is usually appropriate. In your case we can generate lists of the size requested by the user.

from random import randint

pairs = int(input("number of pairs: "))
count = 0
sides = 6
while True:
    count += 1
    deck = [randint(1, sides) for _ in range(pairs)]
    if len(set(deck)) == 1: # if all equal, the set will have 1 value
        break

print(count)
tdelaney
  • 73,364
  • 6
  • 83
  • 116
0

You could use python sets to easily solve this problem.

import random

sides = int(input("How many Dice Sides/Numbers: "))
pairs = int(input("How many Pairs to Search for: "))

time = 0

while True:
    time += 1
    s = {random.randint(1, sides) for _ in range(pairs)}
    if len(s) == 1:
        break

input(f"Found a Pair after {time} times.")

The idea is to generate all rolls and store in set (which holds only unique). Then check for set length to be one, implies all rolls produced equal number.

Yogesh
  • 658
  • 8
  • 20