2012-10-26 52 views
-1

我在python中做了這個骰子游戲,但是我的inputdice函數出現語法錯誤。下面是整個骰子游戲。運行時,遊戲應該經過10輪,並在第10輪後停止或用戶用完了錢。有什麼建議麼?骰子游戲中的語法錯誤

from random import * 

def dice1(): 
    print("+-----+") 
    print("|  |") 
    print("| * |") 
    print("|  |") 
    print("+-----+") 

def dice2(): 
    print("+-----+") 
    print("|* |") 
    print("|  |") 
    print("| *|") 
    print("+-----+") 

def dice3(): 
    print("+-----+") 
    print("|* |") 
    print("| * |") 
    print("| *|") 
    print("+-----+") 

def dice4(): 
    print("+-----+") 
    print("| * * |") 
    print("|  |") 
    print("| * * |") 
    print("+-----+") 

def dice5(): 
    print("+-----+") 
    print("|* *|") 
    print("| * |") 
    print("|* *|") 
    print("+-----+") 

def dice6(): 
    print("+-----+") 
    print("|* *|") 
    print("|* *|") 
    print("|* *|") 
    print("+-----+") 

def drawdice(d): 
    if d==1: 
     dice1() 
    elif d==2: 
     dice2() 
    elif d==3: 
     dice3() 
    elif d==4: 
     dice4() 
    elif d==5: 
     dice5() 
    elif d==6: 
     dice6() 
    print() 

def inputdie(): 
    dice=input(eval("Enter the number you want to bet on --> ")) 
    while dice<1 or dice>6: 
     print("Sorry, that is not a good number.") 
     dice=input(eval("Try again. Enter the number you want to bet on --> ")) 
    return dice 

def inputbet(s): 
    bet=input(eval("What is your bet?")) 
    while bet>s or bet<=0: 
     if bet>s: 
      print("Sorry, you can't bet more than you have") 
      bet=input(eval("What is your bet?")) 
     elif bet<=0: 
      print("Sorry, you can't bet 0 or less than 0") 
      bet=input(eval("What is your bet?")) 
    return bet 

def countmatches(numbet,r1,r2,r3): 
    n=0 
    if numbet==r1: 
     n+=1 
    if numbet==r2: 
     n+=1 
    if number==r3: 
     n+=1 
    return n 


def payoff(c,betam): 
    payoff=0 
    if c==1: 
     print("a match") 
     payoff=betam 
    elif c==2: 
     print("a double match!") 
     payoff=betam*5 
    elif c==3: 
     print("a triple match!") 
     payoff=betam*10 
    else: 
     payoff=betam*(-1) 
    return payoff 


def main(): 
    dollars=1000 
    rounds=1 
    roll=0 
    single=0 
    double=0 
    triple=0 
    misses=0 
    flag=True 
    print("Play the game of Three Dice!!") 
    print("You have", dollars, "dollars to bet with.") 
    while dollars>0 and rounds<11 and flag==True: 
     print("Round", rounds) 
     dicebet=inputdie() 
     stake=inputbet(dollars) 
     for roll in randrange(1,7): 
      roll1=roll 
     for roll in randrange(1,7): 
      roll2=roll 
     for roll in randrange(1,7): 
      roll3=roll 
     drawdice(roll1) 
     drawdice(roll2) 
     drawdice(roll3) 
     matches=countmatches(dicebet,roll1,roll2,roll3) 
     dollarswon=payoff(matches,stake) 
     if matches==1: 
      single+=1 
     elif matches==2: 
      double+=1 
     elif matches==3: 
      triple+=1 
     elif matches==0: 
      misses+=1 
     if dollarswon>0: 
      print("You got a match!") 
      print("You won $", dollarswon, sep='') 
      dollars=dollars+dollarswon 
      print("Your stake is $", dollars, sep='') 
     else: 
      print("You lost your bet! $", stake, sep='') 
      dollars=dollarswon+dollars 
     rounds+=1 
    if rounds==10: 
     print("*******Singles", single, "Doubles", double, "Triples", triple, "Misses", misses) 
     answer=input("Want to play some more? (y or n)") 
     if answer=="y": 
      main() 
     else: 
      print("Have a good day") 

main() 

任何幫助表示讚賞。謝謝!

+1

將您的問題發佈錯誤。還要描述代碼與您希望執行的操作的比較。 –

+0

當我嘗試運行你的代碼時,我的python解釋器(2.7.2)對'print'中的'sep ='''抱怨。一旦刪除代碼運行,但在'eval(「...」)'上失敗。你想在那裏做什麼? 'eval(expression)':從文檔中:'表達式參數被解析並評估爲一個Python表達式' – roberkules

+0

除去輸入消息周圍的eval(),代碼運行直到行〜120: ' 1,7): roll1 = roll' 這段代碼應該做什麼? – roberkules

回答

3

最接近的錯誤是,eval()需要一個有效的Python語法表達式;
"Enter the number you want to bet on -->"或此程序中的任何其他字符串都不是有效的python表達式,因此在運行時會產生語法錯誤。

該方案的更廣泛的問題是,eval()是沒有必要的,應該避免。

經驗法則,特別是對初學者來說,「eval()是邪惡的」,應該「永遠」不被使用。
請注意,「從不」在引號中,暗示eval()非常有用的確實有很少的用例。
eval()之所以這麼「危險的盟友」,是因爲它在運行時引入了[通常是用戶提供的]任意的python表達式,而且這種表達可能有一個無效的語法(沒什麼大不了的)或者更糟的是,可能包括相當有害,甚至可能惡意代碼,它會調用主機上執行各種壞事的時候......

這就是說,你不需要的eval()在所有工序中得到的輸入來自input()方法。
我認爲你的意思是不是使用像模式:
myVar = eval(input("Enter some value for myVar variable"))
(即以相反的順序中的eval和輸入)
其實這仍然不是EVAL工作()需要參數,因此,你將需要
myVar = eval(str(input("Enter some value for myVar variable")))
但如上述eval()不保證在這裏。

另一種猜測是,你使用eval()因爲你期望從輸入()的返回是字符串類型,而eval()函數將它變成了與程序邏輯使用整數...
raw_input()是該方法返回一個字符串,並且它似乎是您應該用來避免在用戶鍵入不帶引號和其他無效值的文本時出現運行時錯誤的方法。一個常見的成語來獲取用戶輸入整數值,是一樣的東西

int_in = None 
while int_in == None: 
    str_in = raw_input('some text telling which data is expected') 
    try: 
     int_in = int(str_in) 
    except ValueError: 
     # optional output of some message to user 
     int_in = None 

通常我們把這種邏輯的方法來實現輕鬆重用。

希望這會有所幫助。你似乎在用Python做實用的事情:沒有比學習代碼更好的方法 - 偶爾閱讀文檔和閱讀相關書籍。用於本好書插頭:Python Cookbook by Alex Martelli

+0

不能同意更多:eval是邪惡的! – roberkules

1

改變我對代碼進行:

  • 去除eval()
  • 固定一個錯字(數字代替numbet)
  • 改變了for roll in randrange(1,7): roll1=roll塊只是roll1=randrange(1,7)
  • 刪除了if rounds==10:檢查,因爲它是a)沒有必要和b)無效,因爲在最後一個循環之後舍入將是11
  • 解析Y/N回答字符串

from random import * 

def dice1(): 
    print("+-----+") 
    print("|  |") 
    print("| * |") 
    print("|  |") 
    print("+-----+") 

def dice2(): 
    print("+-----+") 
    print("|* |") 
    print("|  |") 
    print("| *|") 
    print("+-----+") 

def dice3(): 
    print("+-----+") 
    print("|* |") 
    print("| * |") 
    print("| *|") 
    print("+-----+") 

def dice4(): 
    print("+-----+") 
    print("| * * |") 
    print("|  |") 
    print("| * * |") 
    print("+-----+") 

def dice5(): 
    print("+-----+") 
    print("|* *|") 
    print("| * |") 
    print("|* *|") 
    print("+-----+") 

def dice6(): 
    print("+-----+") 
    print("|* *|") 
    print("|* *|") 
    print("|* *|") 
    print("+-----+") 

def drawdice(d): 
    if d==1: 
     dice1() 
    elif d==2: 
     dice2() 
    elif d==3: 
     dice3() 
    elif d==4: 
     dice4() 
    elif d==5: 
     dice5() 
    elif d==6: 
     dice6() 
    print() 

def inputdie(): 
    dice=input("Enter the number you want to bet on --> ") 
    while dice<1 or dice>6: 
     print("Sorry, that is not a good number.") 
     dice=input("Try again. Enter the number you want to bet on --> ") 
    return dice 

def inputbet(s): 
    bet=input("What is your bet?") 
    while bet>s or bet<=0: 
     if bet>s: 
      print("Sorry, you can't bet more than you have") 
      bet=input("What is your bet?") 
     elif bet<=0: 
      print("Sorry, you can't bet 0 or less than 0") 
      bet=input("What is your bet?") 
    return bet 

def countmatches(numbet,r1,r2,r3): 
    n=0 
    if numbet==r1: 
     n+=1 
    if numbet==r2: 
     n+=1 
    if numbet==r3: 
     n+=1 
    return n 


def payoff(c,betam): 
    payoff=0 
    if c==1: 
     print("a match") 
     payoff=betam 
    elif c==2: 
     print("a double match!") 
     payoff=betam*5 
    elif c==3: 
     print("a triple match!") 
     payoff=betam*10 
    else: 
     payoff=betam*(-1) 
    return payoff 


def main(): 
    dollars=1000 
    rounds=1 
    roll=0 
    single=0 
    double=0 
    triple=0 
    misses=0 
    flag=True 
    print("Play the game of Three Dice!!") 
    print("You have", dollars, "dollars to bet with.") 
    while dollars>0 and rounds<11 and flag==True: 
     print("Round", rounds) 
     dicebet=inputdie() 
     stake=inputbet(dollars) 
     roll1=randrange(1,7) 
     roll2=randrange(1,7) 
     roll3=randrange(1,7) 
     drawdice(roll1) 
     drawdice(roll2) 
     drawdice(roll3) 
     matches=countmatches(dicebet,roll1,roll2,roll3) 
     dollarswon=payoff(matches,stake) 
     if matches==1: 
      single+=1 
     elif matches==2: 
      double+=1 
     elif matches==3: 
      triple+=1 
     elif matches==0: 
      misses+=1 
     if dollarswon>0: 
      print("You got a match!") 
      print("You won $", dollarswon) 
      dollars=dollars+dollarswon 
      print("Your stake is $", dollars) 
     else: 
      print("You lost your bet! $", stake) 
      dollars=dollarswon+dollars 
     rounds+=1 

    print("*******Singles", single, "Doubles", double, "Triples", triple, "Misses", misses) 
    answer=str(input("Want to play some more? (y or n)")) 
    if answer=="y": 
     main() 
    else: 
     print("Have a good day") 

main() 
2

首先,確保你使用Python 3:

import sys 
print(sys.version) 

它應該顯示類似3.2.1 (...)

的原因是Python 2和Python 3有一些重要的區別,特別是input()只會像你期望的那樣在Python 3中運行(而在Python 2,您必須使用raw_input()

如果您正在關注本書/教程,請確保您使用的是相似版本的Python。

其次,在一些地方已扭轉了inputeval順序:

dice=input(eval("Enter the number you want to bet on --> ")) 

它應該是:

dice=eval(input("Enter the number you want to bet on --> ")) 

..because input(...)返回像"123"一個字符串,那麼你想用此字符串撥打eval。您目前的代碼是致電eval("Enter the number.."),這是不正確的。

也就是說,你應該幾乎永遠不需要使用eval--使用它有一系列的問題,在Python中很少需要。相反,因爲要拍攝包含數字的字符串,並把它變成一個整數,使用int

dice=int(input("Enter the number you want to bet on --> ")) 

這不僅是不容易的eval的問題,它提供了更好的錯誤消息,當您在輸入無效值