1

I'm having problems passing crapsRoll() to gameCraps(). When I define gameCraps() I get the error:

Redefining name 'crapsRoll' from outer scope.

And on the print statement I get an error that says

crapsRoll has no value.

import random 

#===========================crapsRoll()===============================
def crapsRoll():
  roll = random.randint(2,12)
  return(roll)

#===========================gameCraps()===============================
def gameCraps(crapsRoll):
  crapsRoll = crapsRoll
  if (crapsRoll == 7 or crapsRoll == 11):
    gameState = 1
  elif (crapsRoll == 2 or crapsRoll == 3 or crapsRoll == 12):
    gameState = 2
  else:
    gameState = 3
  return(gameState)

print(gameCraps())
2
  • Do you like crap so much you named 2 functions after it? Commented Sep 18, 2017 at 7:00
  • @cᴏʟᴅsᴘᴇᴇᴅ crapS <-- with an 's' ? Commented Sep 18, 2017 at 7:10

4 Answers 4

2

You don't need to pass it as a argument. Just call it from another function:

def gameCraps():
  crapsRoll = crapsRoll()
  if (crapsRoll == 7 or crapsRoll == 11):
    gameState = 1
  elif (crapsRoll == 2 or crapsRoll == 3 or crapsRoll == 12):
    gameState = 2
  else:
    gameState = 3
  return(gameState)
Sign up to request clarification or add additional context in comments.

2 Comments

This overwrites the function name with the local variable, probably not a good idea
also it doesnt answer the users question, as it doesnt pass the function as an argument.
1

If you insist on keeping this structure, this will work:

import random 

#===========================crapsRoll()===============================
def crapsRoll():
  roll = random.randint(2,12)
  return(roll)

#===========================gameCraps()===============================
def gameCraps(crapsRoll):
  crapsRoll = crapsRoll
  if (crapsRoll == 7 or crapsRoll == 11):
    gameState = 1
  elif (crapsRoll == 2 or crapsRoll == 3 or crapsRoll == 12):
    gameState = 2
  else:
    gameState = 3
  return(gameState)

print(gameCraps(crapsRoll()))

You either have to pass the return value of crapsRoll() to gameCraps(<insert int>) or call crapsRoll() from within gameCraps(), as all functions are registered in the same namespace. I don't know the structure of your code, but I would rather refactor it, than creating such a trainwreck. This is not ment in a bad manner :).

3 Comments

we can definitely pass functions in python as an argument
Updated the answer accordingly. Thanks!
this doesnt answer the users question, as it doesnt pass the function as an argument, just the result
0
#===========================gameCraps()===============================
def gameCraps(crapsRoll):
  print crapsRoll
  if (crapsRoll == 7 or crapsRoll == 11):
    gameState = 1
  elif (crapsRoll == 2 or crapsRoll == 3 or crapsRoll == 12):
    gameState = 2
  else:
    gameState = 3
  return(gameState)

print(gameCraps(crapsRoll()))

This can be done in multiple ways, but calling function just one time will be feasible

Comments

0

You are trying to check whether the function has a value of a number, which it doesnt, you need to call the function to get the result.

To summarize from what I believe are two nearly complete answers:

import random 

#===========================crapsRoll()===============================
def crapsRoll():
  roll = random.randint(2,12)
  return(roll)

#===========================gameCraps()===============================
def gameCraps(rollFunction):
  crapsRollResult = rollFunction()
  if (crapsRollResult  == 7 or crapsRollResult  == 11):
    gameState = 1
  elif (crapsRollResult  == 2 or crapsRollResult  == 3 or crapsRollResult  == 12):
    gameState = 2
  else:
    gameState = 3
  return(gameState)

print(gameCraps(crapsRoll))

this doesnt confuse the roll function with the result from the roll and it doesnt re roll for each condition check.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.