python - 类型错误: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()
解决方案
您希望将变量作为函数参数传递给第二个函数。您可能想阅读此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) 传递的参数
推荐阅读
- android - 为什么 MediatorLiveData 没有观察到两个不同的来源?
- javascript - 将脚本加载到 javascript
- excel - 将多个工作簿中的数据收集到主工作簿中 - 来自单元格的路径
- angularjs - AngularJS:仅在某些数据存在时添加 ng-show 属性
- spring - Spring Feign Client JSON解析错误:非法字符
- git - Git引发简单合并的冲突
- java - 发送没有关键值的改造获取请求
- javascript - 为动态创建的输入标签分配和更新值 - jquery
- .net-core - 使用 Magick.Net 生成的 Pdf 文件的 png 是黑色的 #695
- rust - 如果我在代码的不同位置使用非可变变量,为什么会出现借用错误?