简体   繁体   中英

Recover a predefined variable as input

I'm new to programming and especially to python and I have a compulsory project to do.

I explain: I have a list of objects (they are people), each one with characteristics, each characteristic is based on a scale from 1 to 5 (not important for understanding the problem) so I have 6 men and 6 women predefined as objects, I compare them according to their characteristics, if their characteristics are close they will be more or less a perfect match.

So my goal is to enter the names of the people I enter through an input and then my comparison function compares them. The problem is that when I manually compare by entering the names directly it works but when I insert an input, an error occurs.

I'll let you discover the code below to better understand what I'm saying because I don't know if I'm very understandable but I'd be very happy to have an answer to this problem, thank you :D

PS: Sorry for my spelling mistakes, I don't speak the language very well.

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")

Example

    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

If I write 'compare(Bryan,Dita)' for exemple, It's directly the subjects, it's working but with input it's not :/

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

You're trying to use the input() to determine which variable name to use.

There's no good way to do that. Variable names are for the programmer's knowledge only. However, if what you want is key-value pairs - each name pointing to a candidate - then you can use a dict to store everything, instead of many individual variables:

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),
}

Here, we correspond strings to candidates in a dict , instead of using a bunch of variable names . Where before you would use the variable Bryan , now you would do hommes['Bryan'] to get the same object that you declared earlier.

And unlike variable names, we can use user input to determine which of these to draw from, since input() returns a string:

candidat_homme = hommes[input("Homme : ")]
candidat_femme = femmes[input("Femme : ")]
compare(candidat_homme, candidat_femme)

You need to store your people in dicts:

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. 

u need to use eval funtion

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)

ok sorry edit: check now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM