简体   繁体   English

Python 方法不返回正确的值

[英]Python method doesn't return right value

In order to learn how to use classes in Python, I'm currently coding a card game.为了学习如何使用 Python 中的类,我目前正在编写一个纸牌游戏。 I wanted to test the "wert_addieren" method (this method is supposed to add the value of a card to the player score), so I wrote a simple test program.我想测试“wert_addieren”方法(这个方法应该是把一张牌的值加到玩家分数上),所以我写了一个简单的测试程序。 But here is the problem: The only time the method "wert_addieren" returns the expected result, namely但问题是:方法“wert_addieren”唯一一次返回预期结果,即

spieler_punkte = 2 spieler_punkte = 2

is when I change是我改变的时候

return punkte返回朋克特

to

print(deck, punkte)打印(甲板,朋克特)

I hope someone is able to help me:)我希望有人能够帮助我:)

Full code:完整代码:

class Kartenstapel(object):
    def __init__(self):
        #Karten Abkürzungen: Kreuz = KR, Pik = PI, Herz = HE, Karo = KA
        self.karten = ["2KR", "2PI", "2HE", "2KA", "3KR", "3PI", "3HE", "3KA", "4KR", "4PI", "4HE", "4KA",
                    "5KR", "5PI", "5HE", "5KA", "6KR", "6PI", "6HE", "6KA", "7KR", "7PI", "7HE", "7KA",
                    "8KR", "8PI", "8HE", "8KA", "9KR", "9PI", "9HE", "9KA", "10KR", "10PI", "10HE", "10KA",
                    "bubeKR", "bubePI", "bubeHE", "bubeKA", "koeniginKR", "koeniginPI", "koeniginHE", "koeniginKA", 
                    "koenigKR", "koenigPI", "koenigHE", "koenigKA", "assKR", "assPI", "assHE", "assKA"]

    def wert_addieren(self, deck, punkte):
        hilf = deck[-1]
        if "2" in hilf:
            punkte += 2
        return punkte

    def karte_ziehen(self, deck):
        deck.append(self.karten[0])
        self.karten.pop(0)
        return self.karten, deck

k = Kartenstapel()
spieler = []
spieler_punkte = 0
k.karte_ziehen(spieler)
k.wert_addieren(spieler, spieler_punkte)
print(spieler, "\n", spieler_punkte)

First of all, pop method also returns the popped item, so you can just do首先, pop方法也返回弹出的项目,所以你可以这样做

    def karte_ziehen(self, deck):
        deck.append(self.karten.pop(0))
        return self.karten, deck

Secondly, you are returning the values from the functions but not using them.其次,您从函数返回值但不使用它们。 So do something like -所以做类似的事情 -

k = Kartenstapel()
spieler = []
spieler_punkte = 0
karten, spieler = k.karte_ziehen(spieler)
spieler_punkte = k.wert_addieren(spieler, spieler_punkte)
print(spieler, "\n", spieler_punkte)

I have used the same variables for returned values.我对返回值使用了相同的变量。 You can use different if needed.如果需要,您可以使用不同的。

when you do this k.wert_addieren(spieler, spieler_punkte) you are changing the value of spieler_punkte to 2, so you are not really using return, so you don't really need it.当您执行此操作时k.wert_addieren(spieler, spieler_punkte)您正在将spieler_punkte的值更改为 2,因此您并没有真正使用 return,因此您并不需要它。 if you are already changing the value of the parameters you dont really need to return anything unless it is something that has no connection to the parameters.for example, a function that adds an extra card to a deck doesnt need to return anything, but a function that gives us the total value of all the cards in a deck would return a specific value which you would give to another variable.如果您已经在更改参数的值,则您实际上不需要返回任何内容,除非它与参数无关。例如,在牌组中添加额外卡片的 function 不需要返回任何内容,但是function 给我们一副牌中所有牌的总价值将返回一个特定的值,您将给另一个变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM