我在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()
任何幫助表示讚賞。謝謝!
將您的問題發佈錯誤。還要描述代碼與您希望執行的操作的比較。 –
當我嘗試運行你的代碼時,我的python解釋器(2.7.2)對'print'中的'sep ='''抱怨。一旦刪除代碼運行,但在'eval(「...」)'上失敗。你想在那裏做什麼? 'eval(expression)':從文檔中:'表達式參數被解析並評估爲一個Python表達式' – roberkules
除去輸入消息周圍的eval(),代碼運行直到行〜120: ' 1,7): roll1 = roll' 這段代碼應該做什麼? – roberkules