未定义的功能?

时间:2014-10-15 00:01:49

标签: python

我不确定为什么我会收到游戏未定义的错误:

#!/usr/bin/python

# global variables
wins = 0
losses = 0
draws = 0
games = 0

# Welcome and get name of human player
print 'Welcome to Rock Paper Scissors!!'
human = raw_input('What is your name?')
print 'Hello ',human

# start game
game()

def game():
    humanSelect = raw_input('Enter selection: R - Rock, P - Paper, S - Scissors, Q - Quit:  ')
    while humanSelect not in ['R', 'P', 'S', 'Q']:
        print humanSelect, 'is not a valid selection'
        humanSelect = raw_input('Enter a valid option please')

    return humanSelect

main()

3 个答案:

答案 0 :(得分:4)

因为,在执行语句game()时,您尚未到达语句def game():,因此游戏未定义。

如果您将game()移至def game()之后,则会在main()上收到类似的错误,因为您似乎没有定义一个名为main代码中的任何位置。

答案 1 :(得分:3)

您必须先定义函数game才能调用它。

def game():
    ...

game()

答案 2 :(得分:0)

好的,我今天花了一些时间来修补它,现在有以下几点:

import random
import string

# global variables
global wins
wins = 0
global losses
losses = 0
global draws
draws = 0
global games
games = 0
# Welcome and get name of human player
print 'Welcome to Rock Paper Scissors!!'
human = raw_input('What is your name? ')
print 'Hello ',human

def readyToPlay():
    ready = raw_input('Ready to Play? <Y> or <N> ')
    ready = string.upper(ready)

    if ready == 'Y':
        game()
    else:
        if games == 0:
            print 'Thanks for playing'
            exit
        else:
            gameResults(games, wins, losses, draws) 

    return

def game():

    global games
    games += 1
    human = humanChoice()
    computer = computerChoice()
    playResults(human, computer)
    readyToPlay()


def humanChoice():
    humanSelect = raw_input('Enter selection: R - Rock, P - Paper, S - Scissors:  ')
    while humanSelect not in ['R', 'P', 'S']:
        print humanSelect, 'is not a valid selection'
        humanSelect = raw_input('Enter a valid option please')
    return humanSelect

def computerChoice():
    computerInt = random.randint(1, 3)
    if computerInt == '1':
        computerSelect = 'R'
    elif computerInt == '2':
        computerSelect = 'P'
    else:
        computerSelect = 'S'

    return computerSelect

def playResults(human, computer):

    global draws
    global wins
    global losses

    if human == computer:

        print 'Draw'
        draws += 1

    elif human == 'R' and computer == 'P':

        print 'My Paper wrapped your Rock, you lose.'
        losses += 1

    elif human == 'R' and computer == 'S':

        print 'Your Rock smashed my Scissors, you win!'
        wins += 1

    elif human == 'P' and computer == 'S':

        print 'My Scissors cut your paper, you lose.'
        losses += 1

    elif human == 'P' and computer == 'R':

        print 'Your Paper covers my Rock, you win!'
        wins += 1

    elif human == 'S' and computer == 'R':

        print 'My Rock smashes your Scissors, you lose.'
        losses += 1

    elif human == 'S' and computer == 'P':

        print 'Your Scissors cut my Paper, you win!'
        wins += 1

def gameResults(games, wins, losses, draws):
    print 'Total games played', games
    print 'Wins:  ', wins, ' Losses:  ',losses, ' Draws:  ', draws
    exit

readyToPlay()

我将使用与ready,ready = string.upper(ready)相同的方式将humanSelect变量强制为大写。我今天早些时候遇到了缩进错误,但今晚晚些时候会出现问题。

我有一个问题。是否可以在raw_input函数的()之间使用一个变量,类似于:

if game == 0:
    greeting = 'Would you like to play Rock, Paper, Scissors?'
else:
    greeting = 'Play again?'

ready = raw_input(greeting)