调用函数后全局变量不变

时间:2019-10-12 02:25:41

标签: python-3.x

因此,我正在从事一个副项目,并且坦率地说,我对Python或一般的编码没有很丰富的经验,但我正在尝试制作一个卡片应用程序。调用函数后,我无法更改全局变量

如果我给同一个西装,它会在函数内打印出正确的结果,而不是在外部。我在线上读到,将全球放在首位可以解决此问题,但事实并非如此。任何帮助表示赞赏。

class Hand:
    people = 0
    hand = []
    suited = False
    def deal():
        global people
        global hand
        global suited
        num_1 = input("What is your first card?: ")
        suit_1 = input("What is your suit?: ")
        num_2 = input("What is your second card?: ")
        suit_2 = input("What is your suit?: ")
        people = input("How many people at the table?: ")
        card_1 = [num_1,suit_1]
        card_2 = [num_2,suit_2]
        if card_1[0] > card_2[0]:
            hand = [card_1, card_2]
        else:
            hand = [card_2, card_1]
        if card_1[1] == card_2[1]:
            suited = True
        else:
            suited = False
        print(suited)
    deal()
    print(suited)

我希望在输入相同的花色后,它会更改“花色”的值。

3 个答案:

答案 0 :(得分:0)

您的变量在类内部。尝试将它们移出课堂:

people = 0
hand = []
suited = False

class Hand:
    def deal():
        global people
        global hand
        global suited
        num_1 = input("What is your first card?: ")
        suit_1 = input("What is your suit?: ")
        num_2 = input("What is your second card?: ")
        suit_2 = input("What is your suit?: ")
        people = input("How many people at the table?: ")
        card_1 = [num_1,suit_1]
        card_2 = [num_2,suit_2]
        if card_1[0] > card_2[0]:
            hand = [card_1, card_2]
        else:
            hand = [card_2, card_1]
        if card_1[1] == card_2[1]:
            suited = True
        else:
            suited = False
        print(f"suited: {suited}")
    deal()
    print(f"suited: {suited}")
print(f"suited: {suited}")
(py36) [~]$ python3 card_deal.py
What is your first card?: K
What is your suit?: H
What is your second card?: A
What is your suit?: H
How many people at the table?: 7
suited: True
suited: True
suited: True

答案 1 :(得分:0)

我不明白您为什么使用class。您可以使用函数和全局变量使它像下面的代码一样工作。,?

people = 0
hand = []
suited = False

def deal():
    # You can put your global variables in one line
    global people, hand, suited
    num_1 = input("What is your first card?: ")
    suit_1 = input("What is your suit?: ")
    num_2 = input("What is your second card?: ")
    suit_2 = input("What is your suit?: ")
    people = input("How many people at the table?: ")
    card_1 = [num_1,suit_1]
    card_2 = [num_2,suit_2]
    if card_1[0] > card_2[0]:
        hand = [card_1, card_2]
    else:
        hand = [card_2, card_1]
    if card_1[1] == card_2[1]:
        suited = True
    else:
        suited = False
    print(suited)

deal()
print(suited)

输出如下所示,

What is your first card?: K
What is your suit?: H
What is your second card?: A
What is your suit?: H
How many people at the table?: 7
suited: True
suited: True

但是,您尝试使用 Class 来做到这一点。尝试以下代码。它使用静态方法和类变量。但是,如果您是python的新手,并且不了解 OOP ,我建议您遵循上面的功能代码

class Hand():
    # Variables which are belongs to Hand class
    # You can access them use 'Hand.<var>'
    # As Example : 'Hand.people'
    people = 0
    hand = []
    suited = False

    @staticmethod
    def deal():
        # num_1, suit_1, num_2, suit_2, card_1, card_2 are the variables
        # which are belangs to 'deal' method

        num_1 = input("What is your first card?: ")
        suit_1 = input("What is your suit?: ")
        num_2 = input("What is your second card?: ")
        suit_2 = input("What is your suit?: ")

        Hand.people = input("How many people at the table?: ")
        card_1 = [num_1,suit_1]
        card_2 = [num_2,suit_2]
        if card_1[0] > card_2[0]:
            Hand.hand = [card_1, card_2]
        else:
            Hand.hand = [card_2, card_1]
        if card_1[1] == card_2[1]:
            Hand.suited = True
        else:
            Hand.suited = False

Hand.deal()
print(Hand.people)
print(Hand.hand)
print(Hand.suited)

输出是这样的,

What is your first card?: K
What is your suit?: H
What is your second card?: A
What is your suit?: H
How many people at the table?: 7
7
[['K', 'H'], ['A', 'H']]
True

手部课程中, 我们使用handsuitedpeople变量是类变量,而deal方法是静态方法。属于手部类

查看以下资源,
Python3 DOCS about class variables
realpython.com tutorial about static methods

答案 2 :(得分:0)

您要在类内部使用全局变量,只需将属性分配给该类即可。

class Hand:
    people = 0
    hand = []
    suited = False

    @classmethod
    def deal(cls):
        num_1 = input("What is your first card?: ")
        suit_1 = input("What is your suit?: ")
        num_2 = input("What is your second card?: ")
        suit_2 = input("What is your suit?: ")
        cls.people = input("How many people at the table?: ")
        card_1 = [num_1,suit_1]
        card_2 = [num_2,suit_2]
        if card_1[0] > card_2[0]:
            cls.hand = [card_1, card_2]
        else:
            cls.hand = [card_2, card_1]
        if card_1[1] == card_2[1]:
            cls.suited = True
        else:
            cls.suited = False



def main():
    h = Hand()
    h.deal()
    print(h.suited)
    print(h.people)

if __name__ == "__main__":
    main()

相同西装的输出:

What is your first card?: 11
What is your suit?: 5
What is your second card?: 15
What is your suit?: 5
How many people at the table?: 5
True
5