首页 > 解决方案 > 类型错误:deckcards() 缺少 1 个必需的位置参数:“王牌”

问题描述

我正在制作一个顶级王牌游戏,并希望在不同的函数中使用一个局部变量。这是所有必要的代码。hello() 是菜单,但在这里不相关。

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()

标签: pythonpython-3.x

解决方案


您希望将变量作为函数参数传递给第二个函数。您可能想阅读此https://www.pythoncentral.io/fun-with-python-function-parameters/

例如:

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

num_cards 是调用deck_cards(52) 传递的参数


推荐阅读