gpt4 book ai didi

python - 如何传递字符串作为对象名称?

转载 作者:行者123 更新时间:2023-11-30 21:56:12 25 4
gpt4 key购买 nike

这是我最终解决问题的方法:

我创建了两个列表,一个包含对象,另一个包含对象名称(字符串)。然后我编写代码以确保对象及其名称同时附加到两个列表中。这样我就可以轻松地使用 ObjectList[NameList.index(Name)] 调用对象,类似于使用 NameList[ObjectList.index(Object)] 调用名称。

我不知道这是否是最好的解决方案。当我对 python 有更多了解时,也许我会找到更好的方法来做到这一点。

感谢大家的帮助。

<小时/>

我更新了下面的代码。

<小时/>

我正在尝试制作一款可以接受用户输入的游戏,根据该输入创建新对象,并将该对象与现有的对象网络连接起来。

所以我有初始对象:Adam = Human("Male","God","God")Eve = Human("Female", "God", "上帝”)

但是在 AdamEve 之后,我想创建像 Josh = Human("Male", Adam, Eve) 这样的对象,在这里Josh 的属性变成了一个字符串和两个对象,而不是三个字符串。但如果这有效,我可以创建一个对象网络,其中每个对象子对象(亚当和夏娃除外)都有对象父对象。

如果有人对此有任何建议,请告诉我。

<小时/>

我想传递一个用户输入字符串作为某个类的新对象的名称。我不能使用 eval() 因为它很危险。我能做什么?

我是 python3 的新手,正在创建一个小游戏只是为了练习。我创建了一个名为“Human”的类,在游戏中用户应该输入新人类的名称。

我没有尝试太多,因为我发现没有一个问题符合我的问题。到目前为止我只知道我不能使用eval(),因为如果发生eval("import")之类的事情,它可能会造成麻烦。

import random

# list of all humans
Humans = []

# creating the class Human
class Human:

global Humans

def __init__(self, gender, father, mother):
self.gender = gender
self.father = father
self.mother = mother
self.canHaveChild = False
Humans.append(self)

def growup(self):
self.canHaveChild = True


Adam = Human("Male", "God", "God")
Eve = Human("Female", "God", "God")
Humans.append(Adam)
Humans.append(Eve)

# creating the class SpiritualHuman
class SpiritualHuman:

def __init__(self, gend, stparent, ndparent):
self.stparent = stparent
self.ndparent = ndparent
self.gend = gend
self.canHaveChild = False

# haveChild function
def haveChild(Human1, Human2):

gender = ""
gen_pro = random.random()
if gen_pro < 0.5:
gender = "Female"
else:
gender = "Male"

if Human1.canHaveChild & Human2.canHavechild:
if (Human1.gender == "Male") & (Human2.gender == "Female"):
return Human(gender, Human1, Human2)
elif (Human1.gender == "Female") & (Human2.gender == "Male"):
return Human(gender, Human1, Human2)
elif (Human1.gender == "Male") & (Human2.gender == "Male"):
return SpiritualHuman("Yang", Human1, Human2)
else:
return SpiritualHuman("Yin", Human1, Human2)
else:
return "forbidden child"


# a list of all commands
command_list = ["who is the mother of", "who is the father of", "who is the child of", "have child named"]
# user input could be:
# "who is the mother of xxx"
# "who is the father of xxx"
# "who is the child of xxx and xxx"
# "xxx and xxx have child named xxx"



# user input function
def get_input():
command = input(":")
comsplit = command.split()
# check 1st command
if command_list[0] in command:
if comsplit[5] in str(Humans):
print("the mother of", comsplit[5], "is", Humans[str(Humans).index(comsplit[5])].mother())
else:
print(comsplit[5], "does not exist")
# check 2nd command
elif command_list[1] in command:
if comsplit[5] in str(Humans):
print("the father of", comsplit[5], "is", Humans[str(Humans).index(comsplit[5])].father())
else:
print(comsplit[5], "does not exist")
# check 3rd command
elif command_list[2] in command:
if comsplit[5] in str(Humans) and comsplit[7] in str(Humans):
for i in Humans:
if str(i.father()) in [comsplit[5], comsplit[7]] and str(i.mother()) in [comsplit[5], comsplit[7]]:
print(i, "is the child of", comsplit[5], "and", comsplit[7])
else:
print("they don't have a child")
else:
print("at least one of the parents you mentioned does not exist")
# check 4th command
elif command_list[3] in command:
if comsplit[0] in str(Humans) and comsplit[2] in str(Humans):
# here's where the problem is
# I want to use comsplit[7] as name for a new Human object
# how should I do it?

else:
print("at least one of them is not human")
elif command == "humans":
print(str(Humans))
else:
print("invalid command. If you need help, please type 'help'")


while(True):
get_input()

我不知道如何避免错误,但我希望如果用户输入:亚当和夏娃有一个 child ,名叫乔希结果应该是 Josh 是 Human 类的一个对象,其父亲是 Adam,母亲是 Eve。

最佳答案

使用包含您的人类的字典,并将他们的名字作为键:

# global dict, defined at the top of your code
humans = {}

def get_input():
command = input(":").split()
if len(command) == 1:
print(HUMANS) # well, don't know what this one is supposed to be...
elif len(command) > 1:
humans[command[1]] = Human(command[1])
humans[command[2]] = Human(command[2])
humans[command[0]] = haveChild(humans[command[1]], humans[command[2]])

编辑:我刚刚读了你的评论,现在无法完成回答,但简而言之,你必须先将你的父亲和母亲创建为人类,然后才能使用它们,所以你需要改变你创建的方式他们...

关于python - 如何传递字符串作为对象名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55546611/

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