2012-07-17 137 views
2

我寫了一個剛剛停止工作的python腳本。我不知道爲什麼如此任何幫助將不勝感激。控制檯不顯示任何東西。我在底部調用函數開始,但沒有運氣。在空閒Python沒有運行代碼

import random 

year = 1 
our_score = 0 
their_score = 0 
games_played = 0 

#opponent's strategy: 
def op_strategy(): 
    for i in range (0,1): 
     rand = random.randint(0,1) 
    if rand == 0: 
     return "war" 
    if rand == 1: 
     return "peace" 


def start(): 
    global our_score, their_score, year 
    print "=====" 
    print "Year " + str(year) 
    print "Our Score: " + str(our_score) 
    print "Their Score: " + str(their_score) 
    print "" 
    strategy = raw_input("What is your strategy this year? ") 
    inputs(strategy) 

def inputs(strategy): 
    our_score = 0 
    global our_score, their_score, year 
    if str(strategy) == "peace" or str(strategy) == "war": 
     print "You chose: " + str(strategy) 
     op_strat = str(op_strategy()) 
     print "They chose: " + op_strat 
    if str(strategy) == "war" and str(op_strat) == "war": 
     print ">>> Everyoner to arms!" 
     our_score = our_score + 1 
     their_score = their_score + 1 
     year = year + 1 
    elif str(strategy) == "peace" and str(op_strat) == "peace": 
     print ">>> Peace for everyone!" 
     our_score = our_score + 3 
     their_score = their_score + 3 
     year = year + 1 
    elif str(strategy) == "peace" and str(op_strat) == "war": 
     print ">>> They crushed us!" 
     our_score = our_score 
     their_score = their_score + 5 
     year = year + 1 
    elif str(strategy) == "war" and str(op_strat) == "peace": 
     print ">>> We crushed them!" 
     our_score = our_score + 5 
     their_score = their_score 
     year = year + 1 
    if str(year) == "11": 
     print "=====" 
     print "Final" 
     print str(our_score) 
     print str(their_score) 
     if our_score > their_score: 
      print ">>>>> We win! <<<<<" 
     if their_score > our_score: 
      print ">>>>> They win! <<<<<" 
     if their_score == our_score: 
      print ">>>>> It's a tie! <<<<<" 
     play = raw_input("Play again?") 
     if play == "y": 
      start() 
     if play == "n": 
      pass 
     else: 
      play = raw_input('Invalid response. Please enter "y" or "n".') 
    if str(strategy) != "peace" and str(strategy) != "war": 
     strategy = raw_input('Invalid strategy. Enter "peace" or "war": ') 
     inputs(strategy) 
    start() 
start() 
+1

Python恨你。你做了什麼來關閉虛擬機? – duffymo 2012-07-17 23:13:58

+1

你知道如何使用python調試器嗎?你可以自己回答這個問題和類似的問題。 – 2012-07-17 23:14:11

+1

運行代碼時會發生什麼? – Blender 2012-07-17 23:14:25

回答

2

代碼正在執行,但它在raw_input調用中被卡住,並且在打印完成之前不打印,當然由於沒有打印任何內容,用戶不知道這樣做。
緩衝區不會自動刷新。如果您使用-u選項啓動python,則會使用raw_input調用刷新緩衝區,並且提示將很明顯。

1

裝入這件事,你會看到以下錯誤:

SyntaxError: name 'our_score' is assigned to before global declaration (, line 1)

其中一個線:

def inputs(strategy): 
our_score = 0 
global our_score, their_score, year 

至於詳細here:

If the global statement occurs within a block, all uses of the name 
specified in the statement refer to the binding of that name in the top-level 
namespace... i.e. the namespace of the module containing the code block 

你已經分配了一個局部變量our_name然後你告訴函數使用一個同名的全局變量。解決這個問題後應該沒有問題。