我在Python 2.7中为幻想足球制作算法,我用它来比较玩家。我在一个类中创建了所有的actor实例,我希望用户能够选择与算法进行比较的实例:
class QB:
def __init__(self, td, yards, fpts, fptspg, ranking, int):
self.td = td
self.yards = yards
self.fpts = fpts
self.fptspg = fptspg
self.ranking = ranking
self.int = int
Brady = QB(15,4342,234,21,1,3)
Manning = QB(12,1234,433,12, 2,8)
def alg(userinput):
alg = X.td*10+X.yards-X.int*10
print alg
如何让“X”成为用户选择的内容? 谢谢!
答案 0 :(得分:2)
你可以将你的玩家放入字典:
players = dict(
Brady=QB(15,4342,234,21,1,3)
Manning=QB(12,1234,433,12, 2,8)
)
现在,将alg
定义为:
def alg(userinput):
X = players[userinput]
alg = X.td*10+X.yards-X.int*10
print alg
然后它可以被称为:
alg("Brady")
答案 1 :(得分:-2)
也许和eval功能?
class QB:
def __init__(self, td, yards, fpts, fptspg, ranking, int):
self.td = td
self.yards = yards
self.fpts = fpts
self.fptspg = fptspg
self.ranking = ranking
self.int = int
Brady = QB(15,4342,234,21,1,3)
Manning = QB(12,1234,433,12, 2,8)
#print(Brady.td)
def alg(userinput):
X=eval(userinput)
alg = X.td*10+X.yards-X.int*10
print(alg)
.. 实施例
alg("Brady")
结果
4462
或
alg("Manning")
结果
1274