python函数语法错误

时间:2014-09-11 20:39:54

标签: python function

import time
arxi=time.time()

number=(int(input('Dwse arithmo'))

def function1(x):
    for i in range(1,x):
        z1=random.randint(1,6)
        z2=random.randint(1,6)
    if z1=z2:
        w=open("diplo.txt","w")
        print(z1,file=w)
    else:
        f=open("mono.txt","w")
        print(z1,',',z2,file=f)

zaria(number)
f.close()
w.close()  
print("O Xronos einai:",time.time()-arxi)

我得到了这个:

def function1(x): Encountered "def" at line 7, column 1. Was expecting one of:     "(" ...     ")" ...     "[" ...     "," ...     "." ...     "+" ...     "-" ...     "*" ...     "/" ...     "//" ...     "<<" ...     ">>" ...     "%" ...     "^" ...     "|" ...     "&" ...     ">" ...     
 "<" ...     "==" ...     "<=" ...     ">=" ...     "!=" ...     "or" ...     "and" ...     "not" ...     "is" ...     "in" ...     "if" ...     "," ...    

不知道我是否真的失明,或者我的Python安装不顺利。

3 个答案:

答案 0 :(得分:2)

if z1=z2:是您需要的问题==

似乎你有一个缩进问题。这可能只是在网站上:

 for i in range(1,x):
    z1=random.randint(1,6)
    z2=random.randint(1,6)
    if z1==z2:
      w=open("diplo.txt","a") # also you may want to append instead of write?
      print(z1,file=w)
    else:
      f=open("mono.txt","a")   # also you may want to append instead of write?
      print(z1,',',z2,file=f)

答案 1 :(得分:1)

你的括号太多了:

number=(int(input('Dwse arithmo'))
#      ^

一个左括号没有关闭,这里完全是多余的。但是因为它没有被关闭,Python会查找它期望遵循的表达式的其余部分。

除此之外你还有更多问题,因为以下行也会引发错误:

if z1=z2:

您无法在表达式中分配;你可能打算在那里使用==

接下来,您的文件close()来电位置错误; <{1}}和f将不会在到达这些行的时间定义。

完全删除它们并改为使用w语句:

with

with open("diplo.txt", "w") as w:
    print(z1, file=w)

with open("mono.txt","w") as f: print(z1, ',', z2, file=f) 块结束时,现在wf会自动关闭

答案 2 :(得分:1)

你在if语句中做错了。点击此处Python Equality Check Difference您需要使用==语句进行比较。