Python没有运行代码

时间:2012-07-17 23:11:10

标签: 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()

2 个答案:

答案 0 :(得分:2)

代码正在执行,但它停留在raw_input调用,并且在完成之前不会打印,当然用户不知道这样做,因为没有打印。
缓冲区不会自动刷新。如果使用-u选项启动python,则将使用raw_input调用刷新缓冲区,并且提示将很明显。

答案 1 :(得分:1)

空闲中加载它,您将看到以下错误:

  

SyntaxError:名称'our_score'在全局声明(,第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,然后您告诉该函数使用同名的全局变量。修好后应该没有问题。