【问题标题】:Running a main function for several objects为多个对象运行一个主函数
【发布时间】:2013-12-29 15:06:07
【问题描述】:

我在这里遇到了一个棘手的问题。基本上我想做的是有一个类似 tamaguchi 的程序,人们可以选择 tamaguchi 的功能,它的大小要么增加,要么减小。这很有效,因为只有一个玉口!但是应该可以同时创建和管理多个 tamaguchi。不过,当我尝试这个时,我遇到了大问题。到目前为止,基本上这就是我的想法:

class Tamaguchi(object):
    def __init__(self,name,size=1):
        self.name=name
        self.size=size

    def increasesize(self):
        self.size+=1
        GoodBadChange.config(text="Good job. You increased in size!")

    def decreasesize(self):
        self.size-=1
        GoodBadChange.config(text="Bad job. You decreased in size!")

def main():
    print(name)
    root = tkinter.Tk()
    global root
    root.title("Tamaguchi-spelet")
    root.geometry("900x900")
    getLists()
    getPhotos()
    Picture = tkinter.Label(root, image=normal)
    Picture.pack()
    global Picture
    ScoreBoard = tkinter.Label(root, text="Score " + str(tamaguchin.size), font=('Helvetica', 15))
    ScoreBoard.pack()
    global ScoreBoard
    GoodBadChange = tkinter.Label(root, text="", font=('Helvetica', 12))
    GoodBadChange.pack()
    global GoodBadChange
    LatestAction = tkinter.Label(root, text="", font=('Helvetica', 12))
    LatestAction.pack()
    global LatestAction
    app = Application(root)
    app.pack()
    updateDisplay()
    LatestAction.config(text="Hello and welcome to the tamaguchi game!\nThe tamaguchi  starts the day with the size "+str(tamaguchin.size)+"!\nThe tamaguchi starts the day off with the last three actions of "+lista[0]+"-"+lista[1]+"-"+lista[2]+"\n")
    root.mainloop()

tamaguchi_list=[]
amount=int(input("Number of tamaguchis in your farm: "))
for i in range(amount):
    name = input("What do you want to name your tamaguchis?: ")
    tamaguchi_name = Tamaguchi(name)
    tamaguchi_list.append(tamaguchi_name)

for tamaguchis in tamaguchi_list:
    main()

for tamaguchis in tamaguchi_list:
    print(name,"reached a size of ",name.size,"!")

对不起,有点长,我仍然缩短了不相关的部分。但我在想,我们创建一个包含所有 tamaguchi 的列表,然后我们只需为 tamaguchi-list 中的每个 tamaguchi 运行 main 函数。这样,例如“erik”得到一个分数,“mary”得到另一个分数,这应该写在最后。但是,这似乎不起作用,如您所见,我在 main 函数的开头写了“print(name)”,只是为了查看它实际上遍历了列表中的所有名称,但实际上它只是打印了一遍又一遍的同一个名字。我不知道为什么它没有通过所有名称。

另一个问题是,当我想显示 tamaguchi 的大小时,我已经在 str(tamaguchin.size) 之类的主函数中编写了这一事实,但这是因为当我只有一个 tamaguchi 时,我只是在一开始就创建了它,我可以在程序的其余部分中引用它(我曾经拥有的 tamguchin=Tamaguchi('SomeName') !)这可以解决吗?

非常感谢您的帮助,我真的很困惑。

编辑:也许不清楚,因为我没有显示所有代码。我认为它可能太长了,但也许最好理解我的意思! I uploaded it here!

【问题讨论】:

  • 您的列表可能包含对同一个对象的引用,所以难怪所有名称都相同,因为它们实际上是相同的。
  • main 从哪里获得像 name 和更重要的是 tamaguchin 这样的输入?
  • 事实上,主函数引用了“tamaguchin”之类的东西是该程序的遗物,仅适用于一个 tamaguchi。那时我刚开始创建了一个 tamaguchi,它的对象名称为“tamaguchin”。从那里很容易一直引用它。我知道它需要改变,我只是不知道如何或改变什么,如果我想要几个 tamaguchis :o name 来自输入,“name = input("你想给你的 tamaguchis 起什么名字?”)
  • 这很可能是您问题的答案:您在某处定义了一个引用 tamaguchin,它只引用一个 tamaguchi,因此您总是会打印它的名称。为什么不将 tamaguchi 作为main 的输入?否则,tamaguchis in tamaguchi_list: 块的整个根本没有意义,因为您实际上并没有遍历 tamaguchis,即您对它们中的每一个都不做任何事情...您有问题吗?
  • 来自一位瑞典同胞:查看您的完整代码链接......永远不要使用除英语之外的任何东西来表示变量名、函数等。英语是编程的“通用语言”,如果您使用它获得帮助(和工作!)的机会将会增加。你显然对英语没有问题,所以用英语编程也不应该给你带来困难。 Bara som ett 小贴士 :)

标签: python


【解决方案1】:

本章描述了一个游戏,其中玩家由于游戏中的事件导致生物“变大”:http://inventwithpython.com/pygame/chapter6.html

通读游戏设计,类似于增加大小,您可以减小生物的大小。

为了管理多个生物阅读本章:http://inventwithpython.com/pygame/chapter8.html

【讨论】:

    【解决方案2】:

    我需要您更具体地说明您的错误发生在代码的哪几行,才能正确回答这个问题。话虽如此,但是,我建议使用更有效的结构来管理您的 tamaguchins。

    我理解你的描述是“tamaguchi 做某事”然后“tamaguchi 增长或缩小”。

    使用字典尝试这样的事情:

    tamaguchins = {"ralph":10, "george":5, "chester":2}
    
    def size_modify(tamaguchin, plus_minus, tamaguchins):
        tamaguchins[tamaguchin] += plus_minus
        return tamaguchins
    

    这样称呼它:

    tamaguchins = size_modify("ralph", -1, tamaguchins)
    

    或者像这样:

    tamaguchins = size_modify("george", 1, tamaguchins)
    

    无论您喜欢什么,只要记住在这种情况下,您必须始终在调用函数之前使用 'tamaguchins =' 并且您的第三个参数必须始终是 tamaguchins。

    干杯

    【讨论】:

    • 这是否适用于所有名称?我不希望它只适用于“乔治”,它应该适用于用户输入的任何名称。我总是有点害怕这样的硬编码:p 我认为使用字典可能很聪明,因为我可以调用函数 dictionary["size"] 例如,如果那是我想要改变的。我更新了 OP 顺便说一下,它包含了整个代码。
    • just remember that in this case you must always precede the calling of the function with tamaguchins = 这完全是错误的。 dict 对象是可变的,所以不需要返回新的字典。它已就地更改。
    • Hannes 是正确的,不是必须返回字典,但是我仍然建议以这种方式构造和调用函数。这仅仅是因为它使函数更加灵活,可以在列表或字典上执行操作。
    • user3122480,该功能可与上述任何名称一起使用。但是,您当然必须将每个新的 tamaguchi 附加到 tamaguchis 列表中,然后您可以使用相同的索引语法定位任何一个 tamaguchi:size_modify("name", 1/-1, tamaguchis)
    猜你喜欢
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 2012-11-03
    相关资源
    最近更新 更多