-3
import random

pairs = {2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0, 10:0, 11:0, 12:0}

for i in range(0, 1001):

    d1 = random.randrange(0, 7)
    d2 = random.randrange(0, 7)
    summ = d1 + d2
    for k, v in pairs.items():
        if summ == k:
            k[v] += 1
Chris
  • 26,361
  • 5
  • 21
  • 42
  • 4
    Welcome to Stack Overflow. What does "not working" in this case mean? What results are you getting, and what results do you expect? – Chris Sep 01 '22 at 19:48
  • I'm sure `randrange(0, 7)` is wrong. – quamrana Sep 01 '22 at 19:50
  • I expect when summ is equal to the key for ex. summ is 2 then value of the key(2) added with 1 and continue adding until range(1000) reached – Argishti89 Sep 01 '22 at 19:52
  • 2
    Ok, that's what I thought, but what do you get? – quamrana Sep 01 '22 at 19:53
  • Vladimir Fokow answer worked, thank you ! – Argishti89 Sep 01 '22 at 19:57
  • You don't need to loop to find the matching key. Just use `if k in pairs:` – Barmar Sep 01 '22 at 20:00
  • 1
    Welcome to Stack Overflow. Please read [ask] and https://ericlippert.com/2014/03/05/how-to-debug-small-programs/, and try to *read and understand* error messages. In this case, you are told directly that the problem is in the line `k[v] += 1`, and that the problem is because it is not possible to subscript like `k[v]`, because `k` is an integer. The reason `k` is an integer is because it is already the key from the dictionary, not the key itself. If you want to assign into a dictionary, you must subscript the dictionary: `pairs[k] += 1`. That said, there is no reason to iterate like that. – Karl Knechtel Sep 01 '22 at 20:49

3 Answers3

0

What you meant to write was this:

import random

pairs = {2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0, 10:0, 11:0, 12:0}

for i in range(0, 1001):

    d1 = random.randint(1, 6)
    d2 = random.randint(1, 6)
    summ = d1 + d2
    pairs[summ] += 1

print(pairs)

I see that you are searching though the pairs dict to find the right key which is a strange way to find a key. I now see the reason for that was because you are using randrange(0, 7) which produces numbers in the inclusive range of 0..6. This will mean that summ could be 0 or 1 which is not one of the keys contained in pairs.

By using randint(1, 6) this is a lot more like rolling a pair of dice and gives summ in the exact range of the keys you prepared in pairs. This allows my version to just register each roll with pairs[summ] += 1

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • Given that nothing else is done with those values, it might just be: `pairs[random.randint(1, 6) + random.randint(1, 6)] += 1` – Chris Sep 01 '22 at 20:27
  • Yes, that's right, but I left it like this to have some correspondence with the OP's code for easier understanding. – quamrana Sep 01 '22 at 20:35
  • Please don't answer questions caused by simple typos, and please don't infer questions from "I have this code... it is not working". Yes, I misread at first, but the problem is still clearly a typo. Other issue with writing unnecessarily complex code etc. are off topic here; this is **not a discussion forum**. – Karl Knechtel Sep 01 '22 at 20:51
  • @KarlKnechtel: Not a typo by the OP, but yes, I answered in haste before the OP had stated what was wrong. – quamrana Sep 01 '22 at 21:55
  • 1
    The `k[v]` rather than `pairs[k]` isn't, in your view, a typo? You imagine that there is something here that is likely to help future readers? If the question really is "how do I change the value in a dict associated with a given key, given the key and the (irrelevant) existing value?", then surely there is a different duplicate for that. – Karl Knechtel Sep 01 '22 at 21:59
0

You are generating 1000 random dice rolls, then counting them. This can be done efficiently using collections.Counter.

from collections import counter
from random import randint as r

pairs = dict(Counter(r(1, 6) + r(1, 6) for _ in range(1001)))
Chris
  • 26,361
  • 5
  • 21
  • 42
-1

Just change k[v] += 1 to pairs[k] += 1

k is an integer + dictionary key, so the brackets doesn't really make sense in this situation.

You could also just loop through the keys because you aren't really using the value in that dict loop

for k in pairs.keys():
    if summ == k:
        pairs[k] += 1
Nealium
  • 2,025
  • 1
  • 7
  • 9