游戏是你必须匹配3个骰子赢得2英镑或2个骰子才能赢得1英镑。你从5英镑开始,每次花费20便士。
我想得到它,所以它在获胜时将钱加到变量'钱包'上 你去的时候取下20便士。我总是得到错误:在分配之前引用。谢谢。
这是我的代码:
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()
答案 0 :(得分:0)
你正在做wallet= wallet - 0.2
而没有先为钱包设置一个值。
试试这个
wallet = 0
wallet = wallet - 0.2
或通过在每个函数中设置global wallet
来使变量成为全局变量
答案 1 :(得分:0)
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)
嗯,那就是它。但是,它还有很多工作要做:如果你只是在开始错误时按Enter键,如果你没钱,你仍然可以玩。由你来完成这些。