在单独的函数中,即使没有定义,名称也未定义

时间:2018-12-26 10:15:42

标签: python

我在这里有一个更大的程序的一部分:

from random import randint

def computerChooses():
    import random
    choices = ["rock", "paper", "scissors"]
    computerChoice = choices[randint(0,2)]




def whoWins():
    if computerChoice == "rock":
                print("DO THIS")
    elif computerChoice == "paper":
                print("DO THIS")
    elif computerChoice == "scissors":
                print("DO THIS")

computerChooses()
whoWins()

我总是以:

NameError: name 'computerChoice' is not defined

(即使我相信是这样!)

错误消息指出,这是针对第20行和第12行的。

我在哪里做错了,特别是在哪里?

谢谢, hy

1 个答案:

答案 0 :(得分:-1)

from random import randint

def computerChooses():
    import random
    choices = ["rock", "paper", "scissors"]
    computerChoice = choices[randint(0,2)]

    return computerChoice




def whoWins(computerChoice):
    if computerChoice == "rock":
            print("DO THIS")
    elif computerChoice == "paper":
            print("DO THIS")
    elif computerChoice == "scissors":
            print("DO THIS")

result = computerChooses()
whoWins(result)