Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

python programming, functiions help! so confused!?

problem figuring out what checkCave(chosenCave) funtion is. why is there a parameter instead of blank () like most functions? we dont define (chosenCave) and the book explanation below makes NO sense to me i swear ive read it 100 times and it makes zero sense, can someone please explain why this part of the code works?

When we call checkCave(), we will also pass one value to it as an argument. When execution

moves inside the checkCave() function, a new variable named chosenCave will be

assigned this value. This is how we pass variable values to functions since functions cannot read

variables outside of the function (that is, outside of the function's local scope).

Parameters are local variables that get defined when a function is called. The value stored in the

parameter is the argument that was passed in the function call.

def checkCave(chosenCave):

print('You approach the cave...')

time.sleep(2)

print('It is dark and spooky...')

time.sleep(2)

print('A large dragon jumps out in front of you! He opens his jaws and...')

print()

time.sleep(2)

friendlyCave = random.randint(1, 2)

if chosenCave == str(friendlyCave):

print('Gives you his treasure!')

else:

print('Gobbles you down in one bite!')

playAgain = 'yes'

while playAgain == 'yes' or playAgain == 'y':

displayIntro()

caveNumber = chooseCave()

checkCave(caveNumber)

print('Do you want to play again? (yes or no)')

playAgain = input()

Update:

so where on earth does (chosenCave) come from? i even put in another word (tigers) and that works too? why is the word in the function () and what does it do? sorry, but the way this is worded to me is very confusing. it says it will pass one value to it as an argument? so is chosenCave the value? but its not defined in the code? and it says its a paramater, but then it says argument before that? PLEASE HELP ME.

1 Answer

Relevance
  • Anonymous
    7 years ago
    Favorite Answer

    checkCave(caveNumber) this is the key. U pass cavenumber to the function checkcave which for its own use is stored as chosencave. Parameter is chosencave. Arguement is cavenumber if I remember correctly. Its fairly common otherwise every variable ud want to use in any other function needs to be global and that can be tons of variables

Still have questions? Get your answers by asking now.