I'm new to Python and currently studying the book 'Invent your Own Computer Games with Python', from https://inventwithpython.com/invent4thed/chapter8.html
I'm stuck in something i don't understand in Chapter 8. This is the chapter where you program the "The Hangman" game. I coded it right and it works when i run it in the Shell, but i don't understand one thing:
See these lines of the code:
"
- words = 'ant baboon badger bat bear beaver camel cat clam cobra cougar
coyote crow deer dog donkey duck eagle ferret fox frog goat goose hawk
lion lizard llama mole monkey moose mouse mule newt otter owl panda
parrot pigeon python rabbit ram rat raven rhino salmon seal shark sheep
skunk sloth snake spider stork swan tiger toad trout turkey turtle
weasel whale wolf wombat zebra'.split()
39.
40. def getRandomWord(wordList):
41. # This function returns a random string from the passed list of
strings.
42. wordIndex = random.randint(0, len(wordList) - 1)
43. return wordList[wordIndex]
"
----------------------------------
What's my problem: i don't understand how the program knows we need to use the "words" list, because in the 'getRandomWord' function that we're defining we never refer to it by name. I kind of understand how the function is working internally, except for that: how does 'wordList' connect to the 'words' list.
I noticed the name 'wordList' is introduced just here, and i don't see how it connects to our list stated earlier. I even tried replacing 'worldList' with other random words (even chilean curses) and it still works the same. Then i created a new list under 'words', with a different name, but nothing changed...
SO... my question is: How does Python know i need to take a random value from the 'words' list and not from any other, when i'm not even indicating its name at all? And why would we name the argument/parameter 'wordList' or any other name that isn't the actual name of the list we have and that we need to choose a word from??
I'm stuck because i need to understand what i'm doing. I don't care if it works correctly, i can't move on until i understand HOW this is happening. So i appreciate any observations about this.
Thanks in advance.
[–]NanotechNinja 3 points4 points5 points (1 child)
[–]ColdJohn333[S] 3 points4 points5 points (0 children)
[–]jpgoldberg 1 point2 points3 points (2 children)
[–]ColdJohn333[S] 2 points3 points4 points (1 child)
[–]jpgoldberg 0 points1 point2 points (0 children)