所以我用 python 创建了这个水果机,当我调用变量来检查机器的结果时,子例程返回一个非类型错误
我尝试用几种方法将返回的变量强制转换为浮点数(我需要它是什么),但没有成功 我不知道还能尝试什么:我浏览了很多网站并向朋友寻求帮助,但都没有成功;我尝试重写函数的调用方式但没有改变;返回函数如何返回奖金变量,但注意事项已更改。我复制了下面的代码。
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")
如有任何帮助,我们将不胜感激
您的逻辑到达
prizecheck()
的末尾而不返回任何内容,将winnings
设置为None
。
尝试在那里放置调试语句,看看何时会发生这种情况。
每次更改时尝试打印
change
的值,以便可以看到图案。
养成在期望返回值的函数末尾始终添加
return
的习惯。
Python 不是静态类型语言,因此您不能强制值是浮点数。如果可以(用另一种语言),您必须指定函数的返回类型,并在此过程中出现编译器错误。您可以提供类型提示来检查此类错误,但 Python 变量始终可以设置为
None
。