我是编程(尤其是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
如果我以“ compare(Bryan,Dita)”为例,那直接是主题,就可以了,但是输入却不是:/
答案 0 :(得分: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()
来确定要使用的变量名称。
没有好的方法可以做到这一点。变量名仅用于程序员的知识。但是,如果您想要的是键值对-每个名称都指向一个候选人-那么您可以使用dict
来存储所有内容,而不是存储许多单独的变量:
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),
}
在这里,我们将字符串与dict
中的候选者相对应,而不是使用一堆变量名称。在使用变量Bryan
之前,现在要进行hommes['Bryan']
来获得与先前声明的对象相同的对象。
与变量名不同,我们可以使用用户输入来确定从中提取变量名,因为input()
返回一个字符串:
candidat_homme = hommes[input("Homme : ")]
candidat_femme = femmes[input("Femme : ")]
compare(candidat_homme, candidat_femme)
答案 1 :(得分:1)
您需要将您的人员存储在字典中
open class Stock (
@PrimaryKey var stockId: String = UUID.randomUUID().toString(),
var stockSymbol: String = "",
var stockPrice: Double = 0.0
): RealmObject()
val realm = Realm.getDefaultInstance()
realm.executeTransaction { realm ->
val stock = realm.createObject(Stock::class.java, UUID.randomUUID().toString())
stock.stockSymbol = "str"
stock.stockPrice = 2.0
}
答案 2 :(得分:-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)
好的,抱歉,编辑:现在检查