2016-05-18 69 views
2

調用類我很新的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

我認爲字典在這裏是一個更合適的數據結構。 – nekomatic

+0

好吧,我會閱讀那些像我說的對所有這些都很新穎的人。實際上這是我做的第一個小程序。 –

+0

我覺得這個問題會更適合於[codereview.se] – zondo

回答

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上看到完整的代碼示例。

+0

哦哇非常感謝你Xamuel Schulman我真的很感激它,並且非常翔實我在閱讀這篇文章之前已經開始做第二個了,但是我沒有意識到我可以離開他們在()中,雖然他們必須放在[]再次肯定我在找什麼。 –

+0

是的,我是新來這個網站/應用程序我這樣做,左邊的支票 –

+0

@JesseHinojosa是的。非常感謝。 –