You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python子程序返回NonType错误求助:老虎机代码问题排查

老虎机程序NonType错误修复方案

问题描述

开发Python老虎机程序时,调用变量检测结果出现NonType错误,尝试强制转换float类型、修改函数调用方式、调整return语句等方法均未解决问题。

错误原因分析

  1. 函数无默认返回路径:prizecheck函数仅在满足特定匹配条件时返回值,当所有条件都不触发时,函数默认返回None,最终导致money被赋值为None引发错误。
  2. 重复调用覆盖计算结果:循环中多次调用prizecheck时,每次都传入初始的money值,而非基于上一次计算后的结果,导致之前的奖励/扣除逻辑被覆盖,且最后一次调用若返回None会直接赋值给money。
  3. return语句顺序错误:部分return语句写在print之前,导致提示信息永远无法执行输出。

修复后的完整代码

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":
                    print("The house wins, All money lost")
                    winnings = 0
                    return winnings
                elif tocheck in ["Cherry", "Lemon", "Orange", "Star"]:
                    print("You win £1")
                    winnings += 1
                    return winnings
                elif tocheck == "Bell":
                    print("You win £5")
                    winnings += 5
                    return winnings
            else:
                if tocheck == "Skull":
                    winnings -= 1.00
                    return winnings
                elif tocheck in ["Cherry", "Lemon", "Orange", "Star", "Bell"]:
                    print("You win 50p")
                    winnings += 0.50
                    return winnings
        elif wheel3 == tocheck:
            if tocheck == "Skull":
                print("The house wins, you lost £1")
                winnings -= 1.00
                return winnings
            elif tocheck in ["Cherry", "Lemon", "Orange", "Star", "Bell"]:
                print("You win 50p")
                winnings += 0.50
                return winnings
    elif wheel2 == tocheck and wheel3 == tocheck:
        if tocheck == "Skull":
            print("The house wins, you lost £1")
            winnings -= 1.00
            return winnings
        elif tocheck in ["Cherry", "Lemon", "Orange", "Star", "Bell"]:
            print("You win 50p")
            winnings += 0.50
            return winnings
    # 所有条件不满足时返回原始金额,避免返回None
    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 not finish:
    money -= 0.2
    wheel1 = spin()
    wheel2 = spin()
    wheel3 = spin()
    print(wheel1, "|", wheel2, "|", wheel3)
    
    # 基于上一次计算结果连续调用,保证金额累积正确
    change = prizecheck("Skull", money)
    change = prizecheck("Cherry", change)
    change = prizecheck("Bell", change)
    change = prizecheck("Lemon", change)
    change = prizecheck("Orange", change)
    change = prizecheck("Star", change)
    
    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")

关键修改说明

  • 添加默认返回值:在prizecheck函数末尾添加return winnings,确保无论是否触发匹配条件,函数都返回float类型的金额,彻底避免None。
  • 修正调用逻辑:后续调用prizecheck时传入上一次返回的change值,保证奖励/扣除的计算是连续累积的。
  • 调整return与print顺序:将print语句移到return之前,确保提示信息能正常输出。
  • 简化条件判断:用in关键字替代多值or判断,提升代码可读性。

内容的提问来源于stack exchange,提问作者TheGeek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 10:43:14