2012-07-17 119 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然后你告诉函数使用一个同名的全局变量。解决这个问题后应该没有问题。