So I have created this fruit machine in python and when I call the variable to check the results of the machine the subroutine returns a non type error
I have tried to force the returned variable into a float (what I need it to be ) a couple ways but no luck I am not sure what else to try: I have looked on a multitude of websites and asked friends for help but with no luck; I have tried to rewrite how the function is called but no change; how the return functions return the winnings variable but noting has changed. I have copied the code below.
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")
Any help would be appreciated
Your logic gets to the end of
prizecheck()without returning anything, settingchangetoNonewhen you dochange = prizecheck(...).Try to put a debug statement there and see when this happens.
Try to print the value of
changeevery time you change it so you can see the pattern.Get in the habit to always
returnsomething at the end of functions you expect a return value from.Python is not a statically typed language so you can't force a value to be a float. If you could (in another language), you'd have to specify the return type of the function, and get a compiler error in the process. You can provide a type hint to check for mistakes like this, but a Python variable can always be set to
None.