2014-11-21 38 views
0

我對python真的很陌生,有我認爲是一個非常簡單的問題。我有python腳本goldmedal.py,我想把這個腳本的結果返回給命令行。在Windows命令行中顯示python return

我一直在尋找,但我似乎無法找到一個簡單的答案。當我運行goldmedal.py我想要顯示返回olympic_medal_counts_df到命令行。我應該添加到代碼中,以便將變量返回到命令行。

from pandas import DataFrame, Series 

def create_dataframe(): 

    countries = ['Russian Fed.', 'Norway', 'Canada', 'United States', 
       'Netherlands', 'Germany', 'Switzerland', 'Belarus', 
       'Austria', 'France', 'Poland', 'China', 'Korea', 
       'Sweden', 'Czech Republic', 'Slovenia', 'Japan', 
       'Finland', 'Great Britain', 'Ukraine', 'Slovakia', 
       'Italy', 'Latvia', 'Australia', 'Croatia', 'Kazakhstan'] 

    gold = [13, 11, 10, 9, 8, 8, 6, 5, 4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0] 
    silver = [11, 5, 10, 7, 7, 6, 3, 0, 8, 4, 1, 4, 3, 7, 4, 2, 4, 3, 1, 0, 0, 2, 2, 2, 1, 0] 
    bronze = [9, 10, 5, 12, 9, 5, 2, 1, 5, 7, 1, 2, 2, 6, 2, 4, 3, 1, 2, 1, 0, 6, 2, 1, 0, 1] 

    d = {'countries': Series(countries), 'gold': Series(gold), 'silver':Series(silver), 'bronze': Series(bronze)} 

    olympic_medal_counts_df = DataFrame(d) 

    return olympic_medal_counts_df 
+0

除非你在你沒有顯示的地方調用*'create_dataframe',否則這個腳本沒有結果。 – jonrsharpe 2014-11-21 13:32:18

回答

3

創建一個python文件,使用.py擴展名保存。

然後創建一個main函數

def main(): 
    df = create_dateframe() # Note you need to call your other function 
    print(df) 

if __name__ == "__main__": 
    main() 

然後運行從命令提示的Python腳本。

+0

添加if __name__ ==「__main__」:main()做了訣竅!謝謝,我會在幾分鐘之內接受我的 – phicon 2014-11-21 13:38:06