用用户输入调用类

时间:2016-05-18 12:04:14

标签: class python-3.x methods

我对Python和编程很新。我试图创建一个小程序,告诉你NFL球队的四分卫。我得到了它的工作,但看看是否有一个较少重复的方式这样做有两个原因:

  • 这样我就不必输入那么多了,
  • 因为它会缩短我的代码。

我试图让用户输入插件进入类调用,所以我不必输入这么多并使用大量elif命令,例如:

x= input("")`
print (x.qb,x.num)

这是我到目前为止所拥有的。它现在有效,但我希望有一种更简单的方法来完成它:

class football:
    def __init__(self,qb,num):                                                                                         
        self.qb = qb
        self.num = num

Niners = football("Gabbert", "02" )
Bears = football("CUTLER, JAY","06") 
Bengals = football ("Dalton, Andy","14")
Bills =football (" Taylor, Tyrod", "05")
Broncos =football ("Sanchez, Mark", "06")
Browns =football ("MCCOWN, JOSH", "13")
Bucaneers =football ("Winston, Jameis", "03")
Cardinals =football ("PALMER, CARSON", "03")
Chargers =football ("RIVERS, PHILIP", "17")
Cheifs =football ("SMITH, ALEX", '11')
Colts =football ("Luck, Andrew",' 12' )
Cowboys =football ("Romo,Tony","09")
Dolphins =football ("Tannehill, Ryan", '17' )
Eagles =football ("Bradford, Sam", '07')
Falcons =football ("RYAN, MATT",' 02' )
Giants =football ("MANNING, ELI", '10' )
Jaguars =football ("Bortles, Blake", '05')
Jets =football ("Smith, Geno",' 07' )
Lions =football ("Stafford, Matthew", '09' )
Packers =football ("RODGERS, AARON", '12')
Panthers =football ("Newton, Cam",' 01' )
Patriots =football ("BRADY, TOM", '12')
Raiders =football ("Carr, Derek",' 04')
Rams =football ("Foles, Nick", '05')
Ravens =football ("FLACCO, JOE",' 05')
Redskins =football ("Cousins, Kirk", '08')
Saints =football ("BREES, DREW",' 09' )
Seahawks =football ("Wilson, Russell", '03')
Steelers =football ("ROETHLISBERGER, BEN",' 07')
Texans =football ("Osweiler, Brock", '17')
Titans =football ("Mariota, Marcus",' 08' ) 
Vikings=football ("Bridgewater, Teddy", '05' )

def decor(func):
    def wrap():
        print("===============================")
        func()
        print("===============================")
    return wrap

def print_text():
    print("Who\s your NFL Quarterback? ")

decorated = decor(print_text)
decorated()

team= input(" Enter your teams name here:").lower()

if team == "cowboys":
    print (Cowboys.qb,Cowboys.num) 
elif team == "niners":
    print (Niners.qb,Niners.num)

1 个答案:

答案 0 :(得分:0)

一个好的选择是使用字典来引用football的每个实例,这样可以避免最后的ifelif结构:

class football:
    def __init__(self,qb,num):
        self.qb = qb
        self.num = num

    def __str__(self):
        return self.qb + ", " + self.num
teams = {
"Niners" : football("Gabbert", "02" ),
"Bears" : football("CUTLER, JAY","06"),
"Bengals" : football ("Dalton, Andy","14"),
"Bills" : football (" Taylor, Tyrod", "05")} #etc
#I didn't include the whole dictionary for brevity's sake

def decor(func):
    def wrap():
        print("===============================")
        func()
        print("===============================")
    return wrap

def print_text():
    print("Who\s your NFL Quarterback? ")

decorated = decor(print_text)
decorated()

team = input("Enter your teams name here:").capitalize()
print(teams[team])

您会注意到团队现在在字典中,因此可以通过使用团队名称索引字典来轻松访问每个团队的football实例。这是在语句print(teams[team])的最后一行上完成的。

teams[team]返回与team内存储的密钥关联的值。例如,如果用户输入Chiefs,则字符'Chiefs'将存储在team中。然后,当您尝试使用teams[team]索引字典时,它将访问'Chiefs'的字典条目。

但请注意,teams[team]返回的内容是football个对象。通常情况下,当您打印一个对象时,它会打印出与here上的内容类似的内容,因为它只是打印一些有关该对象的原始信息。让它返回您想要的方法是在类中定义__repr____str__方法(有关该here的更多信息)。这正是我在football类中所做的,因此当打印的类的实例时,它将以所需的格式打印所需的信息。

另一种方法是完全取消该类,并且只需要一个字典,其值为元组,其中包含四分卫的名称及其作为元素的数字。代码看起来有点像这样:

teams = {
"Niners" : ("Gabbert", "02" ),
"Bears" : ("CUTLER, JAY","06"),
"Bengals" : ("Dalton, Andy","14"),
"Bills" : (" Taylor, Tyrod", "05")} #etc
# Again, not including the whole dictionary for brevity's sake

def decor(func):
    def wrap():
        print("===============================")
        func()
        print("===============================")
    return wrap

def print_text():
    print("Who\s your NFL Quarterback? ")

decorated = decor(print_text)
decorated()

team = input("Enter your teams name here:").capitalize()
print(teams[team][0], teams[team][1])

这次teams[team]是一个元组。最后一行是打印第一个元素(四分卫的名字),然后是第二个元素(数字)。

第二个更干净,需要更少的代码,但这确实是个人偏好的问题。

docs中有关于词典的更多信息。

此外,为简洁起见,我缩短了代码示例,但您可以在pastebin上看到完整的代码示例。

相关问题