gpt4 book ai didi

python - 似乎无法调用具有两个类的程序。 Python初学者,希望了解更多关于调用规则的知识

转载 作者:行者123 更新时间:2023-12-01 04:52:29 25 4
gpt4 key购买 nike

我正在处理以下水桶程序,我似乎无法调用该程序,因为它作为两个类,并且第一个类与第二个类一起使用。我只是一个初学者,所以我想知道我在调用过程中做错了什么,以及像我这样的初学者应该注意哪些规则以避免这些问题。

我做了一些研究,但我发现很难适应这个问题的解决方案,因为我不确定当它说“dict”对象没有属性“has_key”时错误意味着什么。如果它可以用更简单的术语表达,因为很多人(包括我)都是 CS 和 Python 的新手。

代码:

class manager:
""" Manage game queue. keep track of states already seen
and who their parent states are"""
def __init__ (self) :
self.queue = []
self.seen = {}

def getState (self) :
"return next state and pop it off the queue"

if not self.queue : return None
state = self.queue[0]
self.queue = self.queue[1:]
return state

def addState (self, parentState, newState) :
"add state if it's new. Remember its parent"
if self.seen.has_key(str(newState)) : return
self.seen[str(newState)] = str(parentState)
self.queue.append (newState)
#print '--- Adding ', newState

def getSolution (self) :
"Return solution from latest state added"
solution = []
state = self.queue[-1]
while state :
solution.append (str(state))
state = self.getParent(state)
solution.reverse()
return solution

def getParent (self, childState) :
"""return parent of state, if it exists"""
try : return self.seen[str(childState)]
except : return None

class bucketPlayer:
def __init__ (self, manager) :
self.manager = manager

def test (self, oldstate, newstate) :
[newA, newB] = newstate
won = (newA == self.goal or newB == self.goal)
self.manager.addState (oldstate, newstate)
return won

def playGame (self, aMax, bMax, goal) :
"grab a state and generate 8 more to submit to the manager"
self.goal = goal
self.manager.addState("", [0,0]) # start with 2 empty buckets
while 1 :
oldstate = self.manager.getState()
[aHas,bHas] = oldstate
if self.test (oldstate, [aMax,bHas]): break # fill A from well
if self.test (oldstate, [0 ,bHas]): break # empty A to well
if self.test (oldstate, [aHas,bMax]): break # fill B from well
if self.test (oldstate, [aHas,0 ]): break # empty B to well
howmuch = min(aHas, bMax-bHas)
if self.test (oldstate, [aHas-howmuch,bHas+howmuch]): break # pour A to B
howmuch = min(bHas, aMax-aHas)
if self.test (oldstate, [aHas+howmuch,bHas-howmuch]): break # pour B to A
print ("Solution is: ")
print (string.join (self.manager.getSolution(), "\n"))


m = manager()
p = bucketPlayer(m)
# A=7 liters, B=11 liters, Want=6
p.playGame(7,11,6)

错误:

Traceback (most recent call last):
File "/Applications/Python 3.4/Portfolio Projects/Project 4.py", line 70, in <module>
p.playGame(7,11,6)
File "/Applications/Python 3.4/Portfolio Projects/Project 4.py", line 51, in playGame
self.manager.addState("", [0,0]) # start with 2 empty buckets
File "/Applications/Python 3.4/Portfolio Projects/Project 4.py", line 18, in addState
if self.seen.has_key(str(newState)) : return
AttributeError: 'dict' object has no attribute 'has_key'

最佳答案

较旧的has_key方法已被废弃并且 was removed completely在 Python 3.x 中

使用 in conditionalif key in dict: 形式相反:

if str(newState) in self.seen : return

关于python - 似乎无法调用具有两个类的程序。 Python初学者,希望了解更多关于调用规则的知识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28119877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com