我在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()
感谢任何帮助。谢谢!
答案 0 :(得分:3)
最近的错误是eval()
期望表达式是有效的python语法;
"Enter the number you want to bet on -->"
或此程序中的任何其他字符串都不是有效的python表达式,因此在运行时会产生语法错误。
该计划的更广泛问题是,eval()
不是必需的,应该避免。
经验法则,特别是对于初学者来说,“eval()是邪恶的”并且应该“永远不会”使用。
请注意,“ never ”在引号中,暗示这样一个事实,即确实存在很少的用例,其中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()
,因为你期望从input()返回的是string类型,而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
答案 1 :(得分:3)
首先,确保您使用的是Python 3:
import sys
print(sys.version)
它应该显示类似3.2.1 (...)
原因是Python 2和Python 3有一些重要的区别,特别是input()
只能按照你在Python 3中的预期行事(而在Python 2中你必须使用raw_input()
代替)
如果您正在阅读一本书/教程,请确保您使用的是类似版本的Python。
其次,在一些地方你已经颠倒了input
和eval
的顺序:
dice=input(eval("Enter the number you want to bet on --> "))
应该是:
dice=eval(input("Enter the number you want to bet on --> "))
..因为input(...)
会返回类似"123"
的字符串,那么您希望使用此字符串调用eval
。您当前的代码正在调用eval("Enter the number..")
,这是不正确的。
那就是说,你几乎不需要使用eval
- 使用它有很多问题,而且在Python中很少需要。相反,由于您想要获取包含数字的字符串,并将其转换为整数,请使用int
:
dice=int(input("Enter the number you want to bet on --> "))
这不仅不容易出现eval
的问题,当您输入无效值时,它会为您提供更好的错误消息
答案 2 :(得分:1)
我对您的代码所做的更改:
eval()
for roll in randrange(1,7): roll1=roll
块更改为roll1=randrange(1,7)
if rounds==10:
支票,因为它是a)没必要,b)无效,因为在最后一个循环后将是11,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()