我需要帮助在我的函数中使用变量

时间:2014-02-07 23:46:35

标签: python function if-statement return global-variables

我遇到的问题是变量没有像我希望的那样在我的函数中定义。我收到错误消息“全局名称color1未定义”,即使我从一个函数返回color1,我给它一个定义的值。 这是我的代码:

    import random   

card = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50','51','52']
def computer_card1():
    return random.choice(card)

def computer_card2():
    return random.choice(card)

def player_color():
        color_in = input('Purple, Black or Red? ').strip().lower()
        return color_in
def cards():
    if computer_card1() == 1:
        color1 = black
        value1 = 2
    if computer_card1() == 2:
        color1 = black
        value1 = 3
    if computer_card1() == 3:
        color1 = black
        value1 = 4
    if computer_card1() == 4:
        color1 = black
        value1 = 5
    if computer_card1() == 5:
        color1 = black
        value1 = 6
    if computer_card1() == 6:
        color1 = black
        value1 = 7
    if computer_card1() == 7:
        color1 = black
        value1 = 8
    if computer_card1() == 8:
        color1 = black
        value1 = 9
    if computer_card1() == 9:
        color1 = black
        value1 = 10
    if computer_card1() == 10:
        color1 = black
        value1 = 11
    if computer_card1() == 11:
        color1 = black
        value1 = 12
    if computer_card1() == 12:
        color1 = black
        value1 = 13
    if computer_card1() == 13:
        color1 = black
        value1 = 0
    if computer_card1() == 14:
        color1 = black
        value1 = 2
    if computer_card1() == 15:
        color1 = black
        value1 = 3
    if computer_card1() == 16:
        color1 = black
        value1 = 4
    if computer_card1() == 17:
        color1 = black
        value1 = 5
    if computer_card1() == 18:
        color1 = black
        value1 = 6
    if computer_card1() == 19:
        color1 = black
        value1 = 7
    if computer_card1() == 20:
        color1 = black
        value1 = 8
    if computer_card1() == 21:
        color1 = black
        value1 = 9
    if computer_card1() == 22:
        color1 = black
        value1 = 10
    if computer_card1() == 23:
        color1 = black
        value1 = 11
    if computer_card1() == 24:
        color1 = black
        value1 = 12
    if computer_card1() == 25:
        color1 = black
        value1 = 13
    if computer_card1() == 26:
        color1 = black
        value1 = 0
    if computer_card1() == 27:
        color1 = red
        value1 = 2
    if computer_card1() == 28:
        color1 = red
        value1 = 3
    if computer_card1() == 29:
        color1 = red
        value1 = 4
    if computer_card1() == 30:
        color1 = red
        value1 = 5
    if computer_card1() == 31:
        color1 = red
        value1 = 6
    if computer_card1() == 32:
        color1 = red
        value1 = 7
    if computer_card1() == 33:
        color1 = red
        value1 = 8
    if computer_card1() == 34:
        color1 = red
        value1 = 9
    if computer_card1() == 35:
        color1 = red
        value1 = 10
    if computer_card1() == 36:
        color1 = red
        value1 = 11
    if computer_card1() == 37:
        color1 = red
        value1 = 12
    if computer_card1() == 38:
        color1 = red
        value1 = 13
    if computer_card1() == 39:
        color1 = red
        value1 = 0
    if computer_card1() == 40:
        color1 = red
        value1 = 2
    if computer_card1() == 41:
        color1 = red
        value1 = 2
    if computer_card1() == 42:
        color1 = red
        value1 = 3
    if computer_card1() == 43:
        color1 = red
        value1 = 4
    if computer_card1() == 44:
        color1 = red
        value1 = 5
    if computer_card1() == 45:
        color1 = red
        value1 = 6
    if computer_card1() == 46:
        color1 = red
        value1 = 7
    if computer_card1() == 47:
        color1 = red
        value1 = 8
    if computer_card1() == 48:
        color1 = red
        value1 = 9
    if computer_card1() == 49:
        color1 = red
        value1 = 10
    if computer_card1() == 50:
        color1 = red
        value1 = 11
    if computer_card1() == 51:
        color1 = red
        value1 = 12
    if computer_card1() == 52:
        color1 = red
        value1 = 13
    if computer_card2() == 1:
        color2 = black
        value2 = 2
    if computer_card2() == 2:
        color2 = black
        value2 = 2
    if computer_card2() == 3:
        color2 = black
        value2 = 4
    if computer_card2() == 4:
        color2 = black
        value2 = 5
    if computer_card2() == 5:
        color2 = black
        value2 = 6
    if computer_card2() == 6:
        color2 = black
        value2 = 7
    if computer_card2() == 7:
        color2 = black
        value2 = 8
    if computer_card2() == 8:
        color2 = black
        value2 = 9
    if computer_card2() == 9:
        color2 = black
        value2 = 10
    if computer_card2() == 10:
        color2 = black
        value2 = 11
    if computer_card2() == 11:
        color2 = black
        value2 = 12
    if computer_card2() == 12:
        color2 = black
        value2 = 13
    if computer_card2() == 13:
        color2 = black
        value2 = 0
    if computer_card2() == 14:
        color2 = black
        value2 = 2
    if computer_card2() == 15:
        color2 = black
        value2 = 3
    if computer_card2() == 16:
        color2 = black
        value2 = 4
    if computer_card2() == 17:
        color2 = black
        value2 = 5
    if computer_card2() == 18:
        color2 = black
        value2 = 6
    if computer_card2() == 19:
        color2 = black
        value2 = 7
    if computer_card2() == 20:
        color2 = black
        value2 = 8
    if computer_card2() == 21:
        color2 = black
        value2 = 9
    if computer_card2() == 22:
        color2 = black
        value2 = 10
    if computer_card2() == 23:
        color2 = black
        value2 = 11
    if computer_card2() == 24:
        color2 = black
        value2 = 12
    if computer_card2() == 25:
        color2 = black
        value2 = 13
    if computer_card2() == 26:
        color2 = black
        value2 = 0
    if computer_card2() == 27:
        color2 = red
        value2 = 2
    if computer_card2() == 28:
        color2 = red
        value2 = 3
    if computer_card2() == 29:
        color2 = red
        value2 = 4
    if computer_card2() == 30:
        color2 = red
        value2 = 5
    if computer_card2() == 31:
        color2 = red
        value2 = 6
    if computer_card2() == 32:
        color2 = red
        value2 = 7
    if computer_card2() == 33:
        color2 = red
        value2 = 8
    if computer_card2() == 34:
        color2 = red
        value2 = 9
    if computer_card2() == 35:
        color2 = red
        value2 = 10
    if computer_card2() == 36:
        color2 = red
        value2 = 11
    if computer_card2() == 37:
        color2 = red
        value2 = 12
    if computer_card2() == 38:
        color2 = red
        value2 = 13
    if computer_card2() == 39:
        color2 = red
        value2 = 0
    if computer_card2() == 40:
        color2 = red
        value2 = 2
    if computer_card2() == 41:
        color2 = red
        value2 = 2
    if computer_card2() == 42:
        color2 = red
        value2 = 3
    if computer_card2() == 43:
        color2 = red
        value2 = 4
    if computer_card2() == 44:
        color2 = red
        value2 = 5
    if computer_card2() == 45:
        color2 = red
        value2 = 6
    if computer_card2() == 46:
        color2 = red
        value2 = 7
    if computer_card2() == 47:
        color2 = red
        value2 = 8
    if computer_card2() == 48:
        color2 = red
        value2 = 9
    if computer_card2() == 49:
        color2 = red
        value2 = 10
    if computer_card2() == 50:
        color2 = red
        value2 = 11
    if computer_card2() == 51:
        color2 = red
        value2 = 12
    if computer_card2() == 52:
        color2 = red
        value2 = 13
    return color1
    return color2
    return value1
    return value2

def main():
    card1 = computer_card1()
    card2 = computer_card2()
    pcolor = player_color()
    print ( "The cards where:",card1,",",card2,",",pcolor )
    if color1 == color2:
        color = color1
    else:
        color = 'purple'
    if color == pcolor:
        print ('Win')
    else:
        print ('Lose')

2 个答案:

答案 0 :(得分:0)

import random

def computer_pick_card():
    face = random.choice(['A', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'])
    suit = random.choice('HSDC')
    return face + suit    # returns eg 'QC' for Queen of Clubs or '7D' for 7 of Diamonds

def player_color():
        return input('Purple, Black or Red? ').strip().lower()

def card_color(card):
    suit = card[-1]    # last character
    if suit in 'CS':
        return 'black'
    elif suit in 'HD':
        return 'red'
    else:
        raise ValueError('bad card suit = {}'.format(suit))

def main():
    pcolor = player_color()

    card1 = computer_pick_card()
    col1  = card_color(card1)
    card2 = computer_pick_card()
    col2  = card_color(card2)
    print("The computer chose {} ({}) and {} ({}).".format(card1, col1, card2, col2))

    if col1 == col2:
        winner = col1
    else:
        winner = 'purple'
    print('Winning color is {}'.format(winner))    

    if pcolor == winner:
        print ('You win!')
    else:
        print ('You lose')

if __name__=="__main__":
    main()

请注意,在这种用法中,实际上挑选两张牌并进行比较会发生大量不必要的工作;你可以直接用

获得相同的结果
winner = random.choice(["red", "black", "purple", "purple"])

答案 1 :(得分:0)

我的天啊!你仍然误解了很多年轻的padawan ...... :)。

可变范围

让我们从variable scope开始吧。让我解释一下。

编写以下代码时,

def myfunction():
    a = 5
    print(a) # OK

b = a # This line fails

你会失败,因为a 本地myfunction,也就是说它只存在于该函数中,最后一个语句引用变量python不知道。这是一个很好的功能,因为没有范围,您必须非常小心,不要在程序的任何可能发生冲突的部分中使用相同的变量名。如果它在一个小程序中很无聊,那么它对于更大的程序是强制性的(想想firefox的10.2M行代码)。

return关键字

return关键字不会在全局范围内放置变量(变量在任何时候都可用的边界)。相反,结束当前函数并将一些值传递给调用者。第一个语句突然结束了该函数,后续return永远不会到达。从调用者那里获取返回值,如下所示:

def afunction():
    a = 5
    return a

b = afunction()
print(b) # will print `5`

Python提供了一种方便的方法,可以以元组的形式从函数中返回多个值。人们就这样使用它:

def somefunction():
    a = 7
    b = "hello"
    return a, b, "worlds"

sev, hel, wo = somefunction()
print(hel, sev, wo) # will print `hello 7 worlds`

Python的禅和所有这些事情

最后一个主题是说好的程序具有简单的算法和复杂性在于数据。 Python有一套很棒的工具,专门用于表达和操作复杂的数据集。 Liststuplesdictionaries用于描述复杂数据,对其进行分组并建立关系。使用它们,您将简化很多代码,因此请尽快学会使用它们。

一些不错的读物: