ShinLeona ShinLeona - 18 days ago 5
Python Question

Stuck creating a dice game

The game is where you have to match 3 dice to win £2 or 2 dice to win £1. You start off with £5 and it costs 20p per go.

I want to get it so it adds the money onto the variable 'wallet' when winning
and taking the 20p off when you have a go. I always get the error: referenced before assignment. Thank You.

This is my code:

import sys, time, random

wallet=5


print("*****************************")
print("**********Dice Game**********")
print("********By ShinLeona*********")
print("*****************************")


def menu():

start=input("Would you like to play?").lower()

if (start[0] == "y"):
wallet= wallet - 0.2
print("Wallet:", wallet ,)

elif (start[0] == "n"):
print("Withdrawing Funds....")
print("Thanks for playing!")
sys.exit()

else:
print("That is not a valid option")
menu()





def diceroll():
randomnumber = random.randint(1,6)
randomnumber2 = random.randint(1,6)
randomnumber3 = random.randint(1,6)

print("Generating dice...")
print(randomnumber)
print(randomnumber2)
print(randomnumber3)

if (randomnumber == randomnumber2) or (randomnumber == randomnumber3) or (randomnumber2 == randomnumber3):
wallet= wallet + 1
print("You've won £1")
menu()

elif (randomnumber and randomnumber2 == randomnumber3) or (randomnumber3 and randomnumber2 == randomnumber) or (randomnumber3 and randomnumber == randomnumber2):
wallet= wallet + 2
print("You've won £2")
menu()

else:
print("You lose")
menu()



menu()
diceroll()

Answer
import sys, time, random

def menu(wallet):
  start=input("Would you like to play?").lower()
  if (start[0] == "y"):
    wallet= wallet - 0.2
    print("Wallet:", wallet ,)
    diceroll(wallet)
  elif (start[0] == "n"):
    print("Withdrawing Funds....")
    print("Thanks for playing!")
    menu(wallet)
  elif (start[0] == "e"):
    sys.exit()
  else:
    print("That is not a valid option")
    menu(wallet)

 def diceroll(wallet):
  randomnumber = random.randint(1,6)
  randomnumber2 = random.randint(1,6)
  randomnumber3 = random.randint(1,6)

  print("Generating dice...")
  print(randomnumber)
  print(randomnumber2)
  print(randomnumber3)

  if (randomnumber == randomnumber2) or (randomnumber == randomnumber3) or (randomnumber2 == randomnumber3):
    wallet = wallet + 1
    print("You've won 1")
    menu(wallet)
  elif (randomnumber and randomnumber2 == randomnumber3) or (randomnumber3 and randomnumber2 == randomnumber) or (randomnumber3 and randomnumber == randomnumber2):
    wallet = wallet + 2 
    print("You've won 2")
    menu(wallet)
  else:
    print("You lose")
    menu(wallet)

wallet=5
menu(wallet)

Well, that's pretty much it. But, it's still much to do: if you just hit Enter at start- error, if you out of money you can still play. It's up to you to finish those.

Comments