返回 NonType 的 Python 子例程

Python subroutine returning NonType

提问人:TheGeek 提问时间:11/3/2023 更新时间:11/3/2023 访问量:52

问:

所以我用python创建了这个水果机,当我调用变量来检查机器的结果时,子例程返回一个非类型错误

我试图通过几种方式将返回的变量强制转换为浮点数(我需要它),但没有运气 我不确定还能尝试什么:我看过很多网站,向朋友寻求帮助,但没有运气;我试图重写函数的调用方式,但没有改变;返回函数如何返回 winnings 变量,但注意已更改。我复制了下面的代码。

import random
money = 1.00
finish = False


print (" You have £", money)

def prizecheck(tocheck,winnings):
    if wheel1 == tocheck:
        if wheel2 == tocheck:
            if wheel3 == tocheck:
                if tocheck == "Skull":
                    winnings = 0
                    return winnings
                    print ("The house wins, All money lost")
                elif tocheck == "Cherry" or tocheck == "Lemon" or tocheck == "Orange" or tocheck == "Star":
                    print ("You win £1")
                    winnings = winnings + 1
                    return winnings
                elif tocheck == "Bell":
                    print ("You win £5")
                    winnings = winnings + 5
                    return winnings
            else:
                if tocheck == "Skull":
                    winnings = winnings - 1.00
                    return winnings
                elif tocheck == "Cherry" or tocheck == "Lemon" or tocheck == "Orange" or tocheck == "Star" or tocheck == "Bell":
                    print ("You win 50p")
                    winnings = winnings + 0.50
                    return winnings
        elif wheel3 == tocheck:
            if tocheck == "Skull":
                print ("The house wins, you lost £1")
                winnings = winnings - 1.00
                return winnings
            elif tocheck == "Cherry" or tocheck == "Lemon" or tocheck == "Orange" or tocheck == "Star" or tocheck == "Bell":
                print ("You win 50p")
                winnings = winnings + 0.50
                return winnings
    elif wheel2 == tocheck:
        if wheel3 == tocheck:
            if tocheck == "Skull":
                print ("The house wins, you lost £1")
                winnings = winnings - 1.00
                return winnings
            elif tocheck == "Cherry" or tocheck == "Lemon" or tocheck == "Orange" or tocheck == "Star" or tocheck == "Bell":
                print ("You win 50p")
                winnings = winnings + 0.50
                return winnings
            
def spin():
    picture = random.randint(1,6)
    if picture == 1:
        wheel = "Cherry"
    elif picture == 2:
        wheel = "Bell"
    elif picture == 3:
        wheel = "Lemon"
    elif picture == 4:
        wheel = "Orange"
    elif picture == 5:
        wheel = "Star"
    else:
        wheel = "Skull"
    return wheel

    


while money >= 0.2 and finish != True:
    money = money - 0.2
    wheel1 = spin()
    wheel2 = spin()
    wheel3 = spin()
    print(wheel1,"|",wheel2,"|",wheel3)
    
    change = prizecheck("Skull",money)
    change = prizecheck("Cherry",money)
    change = prizecheck("Bell",money)
    change = prizecheck("Lemon",money)
    change = prizecheck("Orange",money)
    change = prizecheck("Star",money)
    
    money = change
    
    print("You have £",money)
    
    endcheck = input("Spin again (1), or quit(2)?")
    if endcheck == "2":
        finish = True
    
    if money < 0.20:
        print("Sorry You have ran out of money")



任何帮助将不胜感激

Python 变量 子例程

评论

0赞 matszwecja 11/3/2023
请提供完整的错误回溯。
0赞 Daviid 11/3/2023
当你做但两者都不是 ,或者是 XXXXX 时,你最终会得到prizecheck(XXXXX,money)wheel1wheel2wheel3change = None
0赞 Daviid 11/3/2023
您应该尝试将其转换为 OOP。

答:

1赞 mijiturka 11/3/2023 #1

你的逻辑到达末尾而不返回任何内容,设置为 when you do .prizecheck()changeNonechange = prizecheck(...)

尝试在那里放置一个调试语句,看看什么时候会发生这种情况。

尝试在每次更改时打印值,以便可以看到模式。change

养成总是在你期望从中获得返回值的函数末尾的习惯。return

Python 不是静态类型语言,因此不能强制值为浮点数。如果可以(使用另一种语言),则必须指定函数的返回类型,并在过程中出现编译器错误。您可以提供类型提示来检查此类错误,但 Python 变量始终可以设置为 。None

1赞 Ada 11/3/2023 #2

我认为这应该有效:

import random

money = 1.00
finish = False

print("You have £", money)


def prizecheck(tocheck, money):
    winnings = 0  # Initialize winnings to 0
    if wheel1 == tocheck and wheel2 == tocheck and wheel3 == tocheck:
        if tocheck == "Skull":
            print("The house wins, All money lost")
        elif tocheck in ["Cherry", "Lemon", "Orange", "Star"]:
            print("You win £1")
            winnings = 1
        elif tocheck == "Bell":
            print("You win £5")
            winnings = 5
    elif (wheel1 == tocheck) + (wheel2 == tocheck) + (wheel3 == tocheck) == 2:
        if tocheck == "Skull":
            print("The house wins, you lost £1")
            winnings = -1
        elif tocheck in ["Cherry", "Lemon", "Orange", "Star", "Bell"]:
            print("You win 50p")
            winnings = 0.5
    return money + winnings


def spin():
    picture = random.randint(1, 6)
    if picture == 1:
        wheel = "Cherry"
    elif picture == 2:
        wheel = "Bell"
    elif picture == 3:
        wheel = "Lemon"
    elif picture == 4:
        wheel = "Orange"
    elif picture == 5:
        wheel = "Star"
    else:
        wheel = "Skull"
    return wheel

while money >= 0.2 and not finish:
    money -= 0.2
    wheel1 = spin()
    wheel2 = spin()
    wheel3 = spin()
    print(wheel1, "|", wheel2, "|", wheel3)

    money = prizecheck("Skull", money)
    money = prizecheck("Cherry", money)
    money = prizecheck("Bell", money)
    money = prizecheck("Lemon", money)
    money = prizecheck("Orange", money)
    money = prizecheck("Star", money)

    print("You have £", money)

    endcheck = input("Spin again (1), or quit(2)?")
    if endcheck == "2":
        finish = True

    if money < 0.20:
        print("Sorry, you have run out of money")
  • return 语句之后的任何 print 语句都不会被执行,所以我移动了它们
  • 我还简化了你的陈述,更少的嵌套使它不那么混乱if
  • 每次你要求不同的水果时,你都在覆盖。这意味着只有最后一个果实的结果才会存储在 中。您需要累积所有水果的变化,然后进行相应的更新changeprizecheckchangemoney
  • 在函数中,您使用的是在函数外部定义的名为变量。这会导致您未正确更新 money 变量的问题。您需要传递给函数并返回更新后的值prizecheckwinningsmoney

评论

0赞 Ada 11/3/2023
好点子,编辑了我的答案
2赞 JonSG 11/3/2023 #3

通过查看最频繁的轮子值的计数来考虑如何确定奖金可能会更容易。例如,我相信所有这些都会产生相同的“胜利”,对吧?

["Cherry", "Cherry", "Lemon"]
["Cherry", "Lemon", "Cherry"]
["Lemon", "Cherry", "Cherry"]

如果我们找到最常找到的项目计数,在所有情况下,我们都会得到: 使其更容易检查和处理。("Cherry", 2)

我建议:

def prize_check(wheels, bet_amount):
    ## ---------------------
    ## find the details of the most common item
    ## ---------------------
    most_common_value, most_common_count = collections.Counter(wheels).most_common(1)[0]
    ## ---------------------

    ## ---------------------
    ## All items different
    ## ---------------------
    if most_common_count == 1:
        return bet_amount  # a guess by me at what to return here
    ## ---------------------

    ## ---------------------
    ## Two of the items are the same
    ## ---------------------
    if most_common_count == 2:
        if most_common_value == "Skull":
            return bet_amount - 1.0
        return bet_amount + 0.5
    ## ---------------------

    ## ---------------------
    ## All three the same
    ## ---------------------
    if most_common_value == "Skull":
        return 0

    if most_common_value == "Bell":
        return bet_amount + 5.0

    return bet_amount + 1.0
    ## ---------------------

然后,我们不再多次调用这个新方法,而是调用一次并传入轮盘值和赌注。prizecheck()

money = prize_check([wheel1, wheel2, wheel3], money)

评论

1赞 Sam Mason 11/3/2023
本来也建议使用,但从OPs代码来看,担心这会是太大的跳跃Counter
1赞 JonSG 11/3/2023
@SamMason同意了。希望我的评论能清楚地说明新方法的作用,以便总体效果是 OP 的简化而不是混淆。
1赞 Sam Mason 11/3/2023
当然更清楚了,现在只有十几行实际代码。功能的使用很好,肯定比建议将其转换为“OOP”的评论要好!