[英]File "<string>" SyntaxError: invalid syntax
I've recently been trying to code a program which plays domino with two players and with input, but apparently my syntax is flawed at the line where I define a function.我最近一直在尝试编写一个程序,该程序可以让两个玩家和输入一起玩多米诺骨牌,但显然我的语法在我定义函数的那一行有缺陷。 I think it might be because of my near-zero experience with coding 😅, but after I've searched on the internet, I haven't found any solution.我想这可能是因为我的编码经验几乎为零😅,但是在我在互联网上搜索之后,我没有找到任何解决方案。 Here's the code :这是代码:
def comptage():
occurences_of_numbers=[]
for i in range(7):
a=0
for k in range(7):
if i in list(hand[k]):
a=a+1
occurences_of_numbers.append(a)
return(occurences_of_numbers)
File "<string>", line 36
def comptage():
^
SyntaxError: invalid syntax
This function counts the numbers of occurrences of the numbers (0,1,2,3,4,5,6) in the tuples representing the dominos in the list named hand
.此函数计算在名为hand
的列表中表示多米诺骨牌的元组中数字 (0,1,2,3,4,5,6) 的出现次数。 I then use the function for other functions.然后我将该功能用于其他功能。
available_draws=14
opponent_hand=7
while comptage()[list(board[0])[0]]==0 and comptage()[list(board[len(board)-1])[1]]==0:
inpt_list=list(map(int,input(' Quel domino pioché ?')))
M.append((inpt_list[0],inpt_list[1]))
available_draws=available_draws-1
#board is a tuple list representing the board
def riposte():
probability=[]
for i in range(7):
b=0
for k in range(len(board)):
if i in list(board[k]):
b=b+1
probability.append((7-comptage()[i]-b)/(28-len(hand)-len(board))*(opponent_hand/(opponent_hand+available_draws)))
return(probability)
This error popped up after I changed something in the riposte()
function (I added the b
characteristic).在我更改了riposte()
函数中的某些内容(我添加了b
特性)后弹出了这个错误。 But it shows the mistake for the comptage()
function.但它显示了comptage()
函数的错误。 I don't know why it showed that.我不知道为什么它显示。 I've tried changing the code a bit, rewriting it, and using other variants, but the error message stands still.我尝试稍微更改代码,重写它并使用其他变体,但错误消息仍然存在。
The rest of the code is : (some unfinished stuff)其余的代码是:(一些未完成的东西)
zero=[]
one=[]
two=[]
three=[]
four=[]
five=[]
six=[]
all_the_dominos=[zero,one,two,three,four,five,six]
for i in range(7):
for k in range(7):
all_the_dominos[i].append((i,k))
hand=[]
for i in range(1,8):
input_list=list(map(int,input(' Domino '+str(i)+' ?')))
hand.append((input_list[0],input_list[1]))
print(hand)
for i in range(7):
for k in range(7):
if hand[i] in all_the_dominos[k]:
del all_the_dominos[k][all_the_dominos[k].index(hand[i])]
elif tuple(reversed(list(hand[i]))) in all_the_dominos[k]:
del all_the_dominos[k][all_the_dominos[k].index(tuple(reversed(list(hand[i]))))
board=[(6,1),(1,2),(2,6),(6,6)]
#example of a board
available_draws=14
opponent_hand=7
while input(' Pioche adverse ?')=='oui':
opponent_hand=opponent_hand+1
available_draws=available_draws-1
The only thing that comes after what is problematic is the last bit.唯一出现问题的就是最后一点。
Help would be greatly appreciated.帮助将不胜感激。 😁 😁
!! !! UPDATE !!更新 !!
I've trisd another version of the compatge function, but it still shows the same error :我已经 trisd 另一个版本的 compatge 函数,但它仍然显示相同的错误:
def comptage():
F=[0,0,0,0,0,0,0]
for i in range(7):
for k in range(len(M)):
if i in list(M[k]):
F[i]=F[i]+1
return(F)
But something's strange : i can't even define a variable !但有些奇怪:我什至不能定义一个变量!
a=0
File "<string>", line 34
a=0
^
SyntaxError: invalid syntax
I definitely think there is a problem with my phone because i've downloaded several other coding programs and always got the same error.我肯定认为我的手机有问题,因为我下载了其他几个编码程序并且总是遇到同样的错误。 It's strange because it was working fine and all of a sudden, it doesn't allow me to do anything.这很奇怪,因为它工作正常,突然之间,它不允许我做任何事情。
Well maybe there's just a problem with the code 😂 but i really don't think so.好吧,也许只是代码有问题😂但我真的不这么认为。
在这一行中,您的右括号 ( )
比左括号 ( (
) 多 1 个: P.append((7-comptage()[i]-b)/(28-len(M)-len(J))*(aM/(aM+p)))
Actually, I've found the problem : there was just a missing bracket 😕.实际上,我发现了问题:只是缺少一个括号😕。 I'm new to Python so I didn't pay enough attention even though I thought I did.我是 Python 的新手,所以即使我认为我也没有给予足够的关注。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.