I made this dice game in python, but am getting a syntax error with my inputdice function. Below is the dice game in its entirety. When run, the game should go through 10 rounds and stop after round 10 or when the user runs out of money. Any suggestions?
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()
Any help is appreciated. Thanks!
The proximate error is that
eval()expects an expression that is valid python syntax;"Enter the number you want to bet on -->"or any of the other strings in this program are not valid python expressions, hence the syntax error produced at run time.The broader problem with the program, is that
eval()is not necessary and should be avoided.A rule of thumb, particularly for beginners, is that “eval() is evil” and should “never” be used.
Note that “never” is in quotes, to hint at the fact that there are indeed a [very] few use cases where eval() can be very useful.
The reason why
eval()is such a “dangerous ally” is that it introduces [typically user-provided] arbitrary python expressions at run-time, and there’s a good chance that such expression could have an invalid syntax (no big deal) or worse, could include rather harmful or possibly even malicious code, which when invoked would perform all sorts of bad things on the host…This said, you do not need eval() at all to process the input obtained from the input() method.
I think that you may have meant to use patterns like:
myVar = eval(input("Enter some value for myVar variable"))(i.e. with the eval and input in the reverse order)
Actually this would still not work for eval() requires a string argument, and hence you would have needed
myVar = eval(str(input("Enter some value for myVar variable")))but as said eval() is not warranted here.
Another guess is that you used
eval()because you expected the return from input() to be of type string, and that eval() would turn this into a integer for use with the program logic…raw_input()is the method returning a string, and it is plausibly the one that you should use to avoid getting run-time errors when the user types in text without quotes and other invalid values. A common idiom to get the user to input integer values, is something likeTypically we put this kind of logic in a method for easy reuse.
Hope this helps. You seem to be doing practical things with Python: no better way to learn than to code – along with the occasional review of the documentation and reading of a related book. A plug for good book: Python Cookbook by Alex Martelli