2016-11-15 153 views
-4

遊戲是你必須匹配3個骰子贏得£2或2個骰子贏得£1。你從5英鎊開始,每次去花費20便士。卡住創建一個骰子游戲

我想得到它,所以當贏得 時,它會將錢添加到變量「錢包」上,並在您離開時取消20p。我總是得到錯誤:在賦值之前引用。謝謝。

這是我的代碼:

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

你的問題到底是什麼? – Chris

+3

尋求調試幫助的問題(「爲什麼這個代碼不工作?」)必須包含所需的行爲,特定的問題或錯誤以及在問題本身中重現問題所需的最短代碼。沒有明確問題陳述的問題對其他讀者無益。請參閱:[問]和[mcve] – CollinD

+0

我會建議指出錯誤發生在哪一行。 – CollinD

回答

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) 

井內使變量全球,這幾乎是它。但是,這還有很多事情要做:如果你在開始時輸入錯誤,如果你沒有錢,你仍然可以玩。這取決於你完成這些。

+0

是的,謝謝,沒有完成它 – ShinLeona

0

你不先的值設置爲錢包做wallet= wallet - 0.2

試試這個,而不是

wallet = 0 
wallet = wallet - 0.2 

或通過設置global wallet各功能