2

我写了一个 python 脚本,它突然停止工作。我不确定为什么会有任何帮助。控制台不显示任何内容。我在底部调用了函数 start 但没有运气。

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()
4

2 回答 2

2

代码正在执行,但它被困在 raw_input 调用中,直到它完成才打印,当然用户不知道该怎么做,因为没有打印任何内容。
缓冲区不会自动刷新。如果您使用 -u 选项启动 python,缓冲区将使用 raw_input 调用刷新,并且提示将很明显。

于 2012-07-17T23:53:48.887 回答
1

在Idle中加载它,您将看到以下错误:

SyntaxError: name 'our_score' 在全局声明之前分配(第 1 行)

这些行之一:

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

如此处详述

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,然后您告诉该函数使用同名的全局变量。修复此问题后应该没有问题。

于 2012-07-17T23:41:44.730 回答