0

我是编程新手,尤其是 python,我有一个必修项目要做。

我解释:我有一个对象列表(他们是人),每个对象都有特征,每个特征都基于从 1 到 5 的等级(对于理解问题并不重要)所以我有 6 个男性和 6 个女性预定义为对象,我根据他们的特点进行比较,如果他们的特点接近,他们或多或少会完美匹配。

所以我的目标是通过输入输入我输入的人的姓名,然后我的比较函数将它们进行比较。问题是,当我通过直接输入名称进行手动比较时,它可以工作,但是当我插入输入时,会发生错误。

我会让你发现下面的代码以更好地理解我在说什么,因为我不知道我是否非常容易理解,但我很高兴能得到这个问题的答案,谢谢:D

PS:抱歉我的拼写错误,我的语言不太好。

class Candidat: #Subject class (characteristics)
    def __init__(self,genre,humour,physique,intelligence,projet):
        self.genre=genre
        self.humour=humour
        self.physique=physique
        self.intelligence=intelligence
        self.projet=projet


def compare(Test1,Test2): #comparison function

#Match à 100% :

    if Test1.humour==Test2.humour and Test1.physique==Test2.physique and Test1.intelligence==Test2.intelligence and Test1.projet==Test2.projet:
        print("Match Parfait : 100%")
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
       print("Mauvais Match")

#Match à 75% :

    if Test1.humour!=Test2.humour and Test1.physique==Test2.physique and Test1.intelligence==Test2.intelligence and Test1.projet==Test2.projet:
        print("Match Parfait : 75%")
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        print("Mauvais Match1")

    if Test1.humour==Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence==Test2.intelligence and Test1.projet==Test2.projet:
        print("Match Parfait : 75%")
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        print("Mauvais Match2")

    if Test1.humour==Test2.humour and Test1.physique==Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet==Test2.projet:
        print("Match Parfait : 75%")
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        print("Mauvais Match3")

    if Test1.humour==Test2.humour and Test1.physique==Test2.physique and Test1.intelligence==Test2.intelligence and Test1.projet!=Test2.projet:
        print("Match Parfait : 75%")
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        print("Mauvais Match4")

#Match à 50% :

    if Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence==Test2.intelligence and Test1.projet==Test2.projet:
        print("Match Parfait : 50%")
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        print("Mauvais Match01")

    if Test1.humour!=Test2.humour and Test1.physique==Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet==Test2.projet:
        print("Match Parfait : 50%")
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        print("Mauvais Match02")

    if Test1.humour!=Test2.humour and Test1.physique==Test2.physique and Test1.intelligence==Test2.intelligence and Test1.projet!=Test2.projet:
        print("Match Parfait : 50%")
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        print("Mauvais Match03")

    if Test1.humour==Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet==Test2.projet:
        print("Match Parfait : 50%")
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        print("Mauvais Match04")

    if Test1.humour==Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence==Test2.intelligence and Test1.projet!=Test2.projet:
        print("Match Parfait : 50%")
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        print("Mauvais Match602")

    if Test1.humour==Test2.humour and Test1.physique==Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        print("Match Parfait : 50%")
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        print("Mauvais Match103")


#Match à 25% :

    if Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet==Test2.projet:
        print("Match Parfait : 25%")
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        print("Mauvais Match001")

    if Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence==Test2.intelligence and Test1.projet!=Test2.projet:
        print("Match Parfait : 25%")
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        print("Mauvais Match002")

    if Test1.humour==Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        print("Match Parfait : 25%")
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        print("Mauvais Match003")

    if Test1.humour!=Test2.humour and Test1.physique==Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        print("Match Parfait : 25%")
    elif Test1.humour!=Test2.humour and Test1.physique!=Test2.physique and Test1.intelligence!=Test2.intelligence and Test1.projet!=Test2.projet:
        print("Mauvais Match004")

例子

    Bryan=Candidat("homme",4,5,3,2)
    Adrien=Candidat("homme",4,3,2,3)
    Marin=Candidat("homme",5,2,4,3)
    Alcaraz=Candidat("homme",4,3,2,1)
    Allan=Candidat("homme",4,3,2,1)
    SebyDaddy=Candidat("homme",4,3,2,1)
    #--------------------------------#
    Anissa=Candidat("femme",0,5,2,0)
    Melanie=Candidat("femme",5,3,4,3)
    Dita=Candidat("femme",4,4,3,2)
    LeaMary=Candidat("femme",4,3,2,1)
    Maissane=Candidat("femme",4,3,2,1)
    Kellyn=Candidat("femme",4,3,2,1)

    Candidat=input("Homme : ")#input Man
    Candidat=input("Femme : ")#input Woman
    compare(Test1,Test2)#comparing

例如,如果我写“比较(Bryan,Dita)”,它直接是主题,它正在工作但输入它不是:/

4

3 回答 3

1
Bryan=Candidat("homme",4,5,3,2)
Adrien=Candidat("homme",4,3,2,3)
Marin=Candidat("homme",5,2,4,3)
Alcaraz=Candidat("homme",4,3,2,1)
Allan=Candidat("homme",4,3,2,1)
SebyDaddy=Candidat("homme",4,3,2,1)
#--------------------------------#
Anissa=Candidat("femme",0,5,2,0)
Melanie=Candidat("femme",5,3,4,3)
Dita=Candidat("femme",4,4,3,2)
LeaMary=Candidat("femme",4,3,2,1)
Maissane=Candidat("femme",4,3,2,1)
Kellyn=Candidat("femme",4,3,2,1)

Candidat=input("Homme : ")#input Man
Candidat=input("Femme : ")#input Woman
compare(Test1,Test2)#comparing

您正在尝试使用input()来确定要使用的变量名。

没有好的方法可以做到这一点。变量名仅供程序员了解。但是,如果您想要的是键值对 - 每个名称都指向一个候选者 - 那么您可以使用 adict来存储所有内容,而不是许多单独的变量:

hommes = {
    'Bryan': Candidat("homme",4,5,3,2),
    'Adrien': Candidat("homme",4,3,2,3),
    'Marin': Candidat("homme",5,2,4,3),
    'Alcaraz': Candidat("homme",4,3,2,1),
    'Allan': Candidat("homme",4,3,2,1),
    'SebyDaddy': Candidat("homme",4,3,2,1),
}

femmes = {
    'Anissa': Candidat("femme",0,5,2,0),
    'Melanie': Candidat("femme",5,3,4,3),
    'Dita': Candidat("femme",4,4,3,2),
    'LeaMary': Candidat("femme",4,3,2,1),
    'Maissane': Candidat("femme",4,3,2,1),
    'Kellyn': Candidat("femme",4,3,2,1),
}

在这里,我们将字符串与 a 中的候选项相对应dict,而不是使用一堆变量名。在您使用变量之前Bryan,现在您可以hommes['Bryan']获取之前声明的相同对象。

与变量名不同的是,我们可以使用用户输入来确定要从中提取哪些,因为input()返回一个字符串:

candidat_homme = hommes[input("Homme : ")]
candidat_femme = femmes[input("Femme : ")]
compare(candidat_homme, candidat_femme)
于 2020-10-13T21:17:19.617 回答
1

您需要将人员存储在字典中:

hommes = {}
femmes = {}

Bryan=Candidat("homme",4,5,3,2)
Adrien=Candidat("homme",4,3,2,3)
Marin=Candidat("homme",5,2,4,3)
Alcaraz=Candidat("homme",4,3,2,1)
Allan=Candidat("homme",4,3,2,1)
SebyDaddy=Candidat("homme",4,3,2,1)

hommes[‘Bryan’] = Bryan
hommes[‘Adrien’] = Adrien
hommes[‘Marin’] = Marin
hommes[‘Alcaraz’] = Alcaraz
hommes[‘Allan’] = Allan
hommes[‘SebyDaddy’] = SebyDaddy

#--------------------------------#

Anissa=Candidat("femme",0,5,2,0)
Melanie=Candidat("femme",5,3,4,3)
Dita=Candidat("femme",4,4,3,2)
LeaMary=Candidat("femme",4,3,2,1)
Maissane=Candidat("femme",4,3,2,1)
Kellyn=Candidat("femme",4,3,2,1)

femmes[‘Anissa’] = Anissa
femmes[‘Melanie’] = Melanie
femmes[‘Dita’] = Dita
femmes[‘LeaMary’] = LeaMary
femmes[‘Maissane’] = Maissane
femmes[‘Kellyn’] = Kellyn

homme = input("Homme : ")  #input Man
femme = input("Femme : ")  #input Woman
compare(hommes[homme],femmes[femme]) #comparing


**Update:** I had skipped putting the rest of the people into their dicts. Fixed now. 
于 2020-10-13T21:23:27.077 回答
-1

你需要使用eval功能

replace these lines
Candidat=input("Homme : ")#input Man
Candidat=input("Femme : ")#input Woman

to below lines 
man=eval(input("Homme : "))#input Man
woman=eval(input("Femme : "))#input Woman
compare(man,woman)

好的抱歉编辑:现在检查

于 2020-10-13T21:18:48.480 回答