-3

I don't understand this line:

for symbol, symbol_count in symbols.items():

symbol has never been a variable, is it from the variable symbol_count? If so, how does the code know what symbol is if it has not been a variable or defined?

symbol_count = {
  "A": 2,
  "B": 4,
  "C": 6,
  "D": 8
}

def get_slot_machine_spin(rows, cols, symbols):
  all_symbols = []
  for symbol, symbol_count in symbols.items():
    for _ in range(symbol_count):
      all_symbols.append(symbol)
tripleee
  • 175,061
  • 34
  • 275
  • 318
Jasontor
  • 15
  • 7
  • 1
    I suggest reading https://docs.python.org/3/library/typing.html , as well as the other docs over the `.items()` function of dict types – OneCricketeer Jan 24 '23 at 23:49
  • 1
    [What is a debugger, and how can it help me solve problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – EJoshuaS - Stand with Ukraine Jan 24 '23 at 23:51
  • As a complement to the previous, this question is (also) ripe for the [Rubber Duck Debugging](https://rubberduckdebugging.com) technique. Explain to your duck the exact meaning of each line of code ... using the official Python documentation to help you work it out. For example, `symbol_count` is a `dict` so lookup what `items()` is for a `dict`. Likewise for `append` and `range`. – Stephen C Jan 25 '23 at 00:33
  • (Note that the RDD technique works by you explaining things to your duck, not the other way around. We don't want to be your duck.) – Stephen C Jan 25 '23 at 00:37
  • My code does none of these: When I run my program, it does not produce the output I expect for the input I gave it. When I run my program, it crashes and gives me a stack trace. I have examined the stack trace, but I still do not know the cause of the problem because the stack trace does not provide me with enough information. When I run my program, it crashes because of a segmentation fault (SEGV). I don't need a debugger I need a person to help me understand this one line. @EJoshuaS – Jasontor Jan 25 '23 at 02:08
  • The edit makes this question much clearer and more focused. I voted to reopen because the question is now answerable (although the rude commentary made it rather tempting not to). – EJoshuaS - Stand with Ukraine Jan 25 '23 at 05:18
  • Yes, the edited question is quite good - but it's also a duplicate. – Karl Knechtel Jan 25 '23 at 06:33
  • The loop unpacks the key and value into symbol and symbol_count respectively. symbol will take the value of each key of the symbols dictionary and symbol_count will take the value of the count of each symbol. @KarlKnechtel – Jasontor Jan 25 '23 at 23:31
  • Understanding for loops in Python was not the issue. I had an issue understanding one line. You obviously have something against new programmers. You've done everything you can to stop me from receiving help on this platform @tripleee . I responded to the "rude commentary" from Stephen C nothing more. If you want people just to review the docs then re-direct this site to the docs. I didn't need a debugger. I needed some simple help. That's obviously not possible here. Fortunately I found the help I needed elsewhere, thank you to the people that tried to help I appreciate it. – Jasontor Jan 26 '23 at 23:32
  • I answered your question, and you think I hold a personal grudge somehow? That's new. If you have a different problem which you didn't explain in the question, how are we supposed to be able to help with that? – tripleee Jan 27 '23 at 06:02
  • I explained my issue and you wanted me to update my question, so I did. Then you banned me from asking questions for 7 days because I updated the question you wanted me to update. Then you closed the question stating that it has already been associated with a similar question. It wasn't the same at all. By the time you answered my question you had pushed me to find my answer elsewhere, ChatGPT answered it. When I stated that you deleted the comment. People make snarky comments to new people here and they do it intentionally. You obviously don't want us here. All this for such a simple answer. – Jasontor Jan 27 '23 at 18:06

1 Answers1

1

The for statement creates variables.

for variable (, othervar, ....) in expression:

In this case, the expression is something which returns pairs of values (so basically, a 2-tuple) and so for can declare two variables and have them receive one value each, similarly to how you can say

symbol, symbol_count = "stack overflow", 42

In some more detail, symbols in this case is apparently a dict variable, and items() loops over the keys and returns one key and its value in each iteration.

Quick demo:

>>> symbols = {"foo": 17, "bar": 42, "baz": 1427}
>>> for symbol, symbol_count in symbols.items():
...   print("symbol:", symbol)
...   print("count:", symbol_count)
...
symbol: foo
count: 17
symbol: bar
count: 42
symbol: baz
count: 1427

Because the for loop creates a new variable named symbol_count, it is now a local variable which shadows the global dict with the same name; the two are distinct and unrelated, other than in that they share the same name in two separate symbol tables. Perhaps also read up on global vs local scope in Python.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • I found a duplicate for the question in my saves. Would you mind migrating the answer? The specific sticking point of "what if `symbol` didn't exist before?" isn't well covered there, but I think *that* part is clearly the same question. Although I'm not sure whether the bit about the unpacking syntax (`symbol, symbol_count`) is focused enough. The OP for the other question didn't ask about it. But as I understood it, the question either way is *primarily* about the `for` loop construct itself. – Karl Knechtel Jan 25 '23 at 06:35
  • The red-herring of the shadowed `symbol_count` is definitely out of focus, however. – Karl Knechtel Jan 25 '23 at 06:38
  • @KarlKnechtel Sorry, I can't really see which aspect of this you want migrated. Perhaps you can copy/paste this and use it as the basis for a new answer for the other question; feel free. Also, maybe ping me so I can delete this then. – tripleee Jan 25 '23 at 06:39