python - python 3中self中引用函数的问题
问题描述
我正在处理这段代码,但启动功能没有运行。我没有收到任何错误。这是纸牌游戏,救命?我正在使用 python 3 并在 Windows 10 上通过命令提示符运行。对于我的文本编辑器,我使用的是 Atom。
import random
print("Sam's Casino")
class Game:
def __init__(self):
self.cards = ['1','2','3','4','5','6','7','8','9','10','J',
'Q','K','1','2','3','4','5','6','7','8','9','10','J',
'Q','K','1','2','3','4','5','6','7','8','9','10','J',
'Q','K','1','2','3','4','5','6','7','8','9','10','J',
'Q','K']
self.hand1 = []
self.hand2 = []
self.center = []
self.pile1 = []
self.pile2 = []
self.points1 = 0
self.points2 = 0
input('__init__ completed - ')
self.startup(self)
def deal(self):
self.count = 0
for card in self.cards:
if self.count < 4:
self.hand1.append(card)
self.count += 1
elif self.count > 3 and self.count < 8:
self.hand2.append(card)
self.count += 1
print('hi')
input('deal test - ')
def startup(self):
input('startup __init__ test:')
print("Sam's Casino Version 1.1")
random(self.cards)
print(self.cards)
self.deal(self)
print(self.hand1)
print(self.hand2)
input('self.startup completed - ')
def trick(self):
self.points1_add = input('Points1_add? - ')
self.points2_add = input('Points2_add? - ')
self.points1 += self.points1_add
self.points2 += self.pointd2_add
input('trick test1 - ')
def turn(self):
startup(self)
while self.points1 < 21 and self.points2 < 21:
print('turn test - ')
trick(self)
Key = input('Game()_run? - ')
if Key == 'y':
Game().run()
elif Key == 'n':
print('closing')
input('ctest')
else:
print('invalid input')
input('')
解决方案
如果您没有收到任何错误,那么您需要检查您的工作环境。我显然得到了一个错误。
Traceback (most recent call last):
File "so.py", line 60, in <module>
Game().run()
File "so.py", line 20, in __init__
self.startup(self)
TypeError: startup() takes 1 positional argument but 2 were given
这个调用是多余的:
self.startup(self)
self
是任何实例方法的隐式第一个参数:调用对象成为第一个参数。简单地称它为
self.startup()
这将使您克服第一个结构性问题。
我强烈建议您退回并分小块测试您的代码。您已经编写了超过 50 行代码,而没有测试您的结构。在填写这么多详细信息之前,请测试您的连接性。一次添加几行,测试每个添加。我现在已经修复了三个这样的错误,但仍然出现错误。
推荐阅读
- azure-devops - 如何从 Azure Repos Git 项目的 CI 触发器中排除特定源分支?
- php - 用当前日期 + 14 天填充 HTML5 输入字段
- javascript - 如何发出服务器端 API 请求?
- javascript - 如何在本地渲染小胡子模板
- c - 为什么数组被认为是一种数据结构?
- python - 生成数字直到我得到 7,17,26,28,31,32
- c++ - 为什么 epoll 提供过去事件的数据?
- algorithm - 求 GCD 等于 1 的四联体数
- javascript - 如何使用 Javascript 选择 Xml 数据
- ios - Tableview 单元格未按预期覆盖整个区域