-1

I am making a top trump game and want to get a local variable used in a different function. This is all the necessary code. hello() is the menu but is not relevant here.

def deckcards():
  cards = trumps.cards // 2
  print(cards)

def trumps():
  cards = int(input("""How many cards would you like?
  (Even number from 4, 30): """))
  if cards % 2 == 0 and cards != 0:
    if cards < 4 or cards > 30:
      print("Please enter an even number from 4 to 30")
      hello()

    else:
      deckcards()

  else:
    print("Invalid amount.")
    hello()
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • your code is incomplete. we do not see what `trumps` is. we do not see what `trump.cards` is. why arent you returning `return trumps.cards // 2` from `deckcards()`. Read about [scoping rules](https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules). avoid globals, pass whats needed as function parameters. – Patrick Artner Sep 27 '18 at 20:13
  • Make sure you show the errors and tracebacks as text in the body of the question. Don't hide errors in the title. –  Sep 27 '18 at 20:14

1 Answers1

0

You want to pass the variable as a function parameter to the second function. You may want to read this https://www.pythoncentral.io/fun-with-python-function-parameters/

For example:

def deck_cards(num_cards):
  cards = num_cards // 2
  print(cards)

num_cards is the parameter passed by calling deck_cards(52)

Some Guy
  • 12,768
  • 22
  • 58
  • 86