Hướng dẫn how do i fix invalid syntax in python else? - làm cách nào để sửa cú pháp không hợp lệ trong python khác?

Tôi đang gặp lỗi này:

Show
File "zzz.py", line 70
    else:
       ^
SyntaxError: invalid syntax

Dòng gây ra vấn đề được đánh dấu bằng nhận xét trong mã:

def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules

def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
4 là biến được gán cho lớp "thế giới".

Thành thật mà nói, tôi không biết tại sao tôi có được điều này. Trước khi mọi thứ đều ổn và bây giờ lỗi đó hiển thị sau khi thêm một số hướng dẫn bổ sung trong các khối thụt lề khác.

Bất kỳ ý tưởng?

Tuyên bố và điều kiện def FileParse(self, table_file): vars={} tf = open(table_file, 'r') for line in tf: if line.startswith("#") or line.strip() == "": pass elif line.startswith("n_states:"): self.n_states = str(line[9:].strip()) elif line.startswith("neighborhood:"): self.neighborhood = str(line[13:].strip()) elif line.startswith("symmetries:"): self.symmetries = str(line[11:].strip()) elif line.startswith("var "): line = line[4:] ent = line.replace('=',' ').\ replace('{',' ').\ replace(',',' ').\ replace(':',' ').\ replace('}',' ').\ replace('\n','').split() vars[ent[0]] = [] for e in ent[1:]: if e in vars: vars[ent[0]] += vars[e] else: vars[ent[0].append(int(e))] else: rule = line.strip().split(",") for k in vars.keys(): if k in rule: for i in vars[k]: change = rule.replace(k, i) change = [int(x) for x in change] w.rules.append(Rule(change[:5],change[5]) else: # line which causes the problem rule = [int(x) for x in rule] w.rules.append(Rule(rule[:5],rule[5])) tf.close() self.parse_status "OK" return w.rules 5

def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5 Trong Python có nghĩa là: Chỉ chạy phần còn lại của mã này một lần, nếu điều kiện đánh giá là
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
7. Don Tiết chạy phần còn lại của mã nếu nó không.only run the rest of this code once, if the condition evaluates to
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
7.
Don’t run the rest of the code at all if it’s not.

Giải phẫu của một câu lệnh

def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5: Bắt đầu với từ khóa
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5, theo sau là giá trị boolean, một biểu thức đánh giá thành
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
7 hoặc giá trị với sự thật của Hồi. Thêm một dấu hai chấm
>>> if 3 < 5:
...     print("Hello, World!")
...
Hello, World!
1, một dòng mới và viết mã sẽ chạy nếu câu lệnh là
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
7 dưới một cấp độ thụt.

Hãy nhớ rằng, giống như với các chức năng, chúng ta biết rằng mã được liên kết với một câu lệnh

def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5 theo cấp độ của nó. Tất cả các dòng được thụt vào theo câu lệnh
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5 sẽ chạy nếu nó đánh giá thành
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
7.

>>> if 3 < 5:
...     print("Hello, World!")
...
Hello, World!

Hãy nhớ rằng, các câu lệnh

def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5 của bạn chỉ chạy nếu biểu thức trong chúng đánh giá thành
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
7 và giống như với các chức năng, bạn sẽ cần phải nhập thêm một không gian trong bản phát hành để chạy nó.

Sử dụng
>>> if 3 < 5:
...     print("Hello, World!")
...
Hello, World!
8 với các câu lệnh
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5

Nếu bạn chỉ muốn mã của mình chạy nếu biểu thức là

>>> b = False
>>> if not b:
...     print("Negation in action!")
...
Negation in action!
0, hãy sử dụng từ khóa
>>> if 3 < 5:
...     print("Hello, World!")
...
Hello, World!
8.

>>> b = False
>>> if not b:
...     print("Negation in action!")
...
Negation in action!

def FileParse(self, table_file): vars={} tf = open(table_file, 'r') for line in tf: if line.startswith("#") or line.strip() == "": pass elif line.startswith("n_states:"): self.n_states = str(line[9:].strip()) elif line.startswith("neighborhood:"): self.neighborhood = str(line[13:].strip()) elif line.startswith("symmetries:"): self.symmetries = str(line[11:].strip()) elif line.startswith("var "): line = line[4:] ent = line.replace('=',' ').\ replace('{',' ').\ replace(',',' ').\ replace(':',' ').\ replace('}',' ').\ replace('\n','').split() vars[ent[0]] = [] for e in ent[1:]: if e in vars: vars[ent[0]] += vars[e] else: vars[ent[0].append(int(e))] else: rule = line.strip().split(",") for k in vars.keys(): if k in rule: for i in vars[k]: change = rule.replace(k, i) change = [int(x) for x in change] w.rules.append(Rule(change[:5],change[5]) else: # line which causes the problem rule = [int(x) for x in rule] w.rules.append(Rule(rule[:5],rule[5])) tf.close() self.parse_status "OK" return w.rules 5 Tuyên bố và sự thật

def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5 Các tuyên bố cũng hoạt động với các mục có sự thật của người Hồi giáo đối với họ.

Ví dụ:

  • Số 0 là sai-y, bất kỳ số nào khác (bao gồm cả tiêu cực) là sự thật-y
  • Trống
    >>> b = False
    >>> if not b:
    ...     print("Negation in action!")
    ...
    Negation in action!
    
    4,
    >>> b = False
    >>> if not b:
    ...     print("Negation in action!")
    ...
    Negation in action!
    
    5,
    >>> b = False
    >>> if not b:
    ...     print("Negation in action!")
    ...
    Negation in action!
    
    6 hoặc
    >>> b = False
    >>> if not b:
    ...     print("Negation in action!")
    ...
    Negation in action!
    
    7 là sai
  • Bất kỳ cấu trúc nào trong số đó với các mục trong đó là sự thật-y
>>> message = "Hi there."

>>> a = 0
>>> if a:   # 0 is False-y
...     print(message)
...

>>> b = -1
>>> if b:  # -1 is Truth-y
...     print(message)
...
Hi there.

>>> c = []
>>> if c:  # Empty list is False-y
...     print(message)
...

>>> d = [1, 2, 3]
>>> if d:  # List with items is Truth-y
...     print(message)
...
Hi there.

def FileParse(self, table_file): vars={} tf = open(table_file, 'r') for line in tf: if line.startswith("#") or line.strip() == "": pass elif line.startswith("n_states:"): self.n_states = str(line[9:].strip()) elif line.startswith("neighborhood:"): self.neighborhood = str(line[13:].strip()) elif line.startswith("symmetries:"): self.symmetries = str(line[11:].strip()) elif line.startswith("var "): line = line[4:] ent = line.replace('=',' ').\ replace('{',' ').\ replace(',',' ').\ replace(':',' ').\ replace('}',' ').\ replace('\n','').split() vars[ent[0]] = [] for e in ent[1:]: if e in vars: vars[ent[0]] += vars[e] else: vars[ent[0].append(int(e))] else: rule = line.strip().split(",") for k in vars.keys(): if k in rule: for i in vars[k]: change = rule.replace(k, i) change = [int(x) for x in change] w.rules.append(Rule(change[:5],change[5]) else: # line which causes the problem rule = [int(x) for x in rule] w.rules.append(Rule(rule[:5],rule[5])) tf.close() self.parse_status "OK" return w.rules 5 Báo cáo và chức năng

Bạn có thể dễ dàng khai báo các tuyên bố

def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5 trong các chức năng của mình, bạn chỉ cần chú ý đến mức độ thụt. Lưu ý cách mã thuộc về câu lệnh
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5 được thụt vào ở hai cấp độ.

>>> def modify_name(name):
...    if len(name) < 5:
...         return name.upper()
...    else:
...         return name.lower()
...
>>> name = "Nina"
>>> modify_name(name)
'NINA'

Tuyên bố
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5 lồng nhau

Sử dụng cùng một kỹ thuật, bạn cũng có thể làm tổ các câu lệnh

def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5 của mình.

>>> def num_info(num):
...    if num > 0:
...        print("Greater than zero")
...        if num > 10:
...            print("Also greater than 10.")
...
>>> num_info(1)
Greater than zero
>>> num_info(15)
Greater than zero
Also greater than 10.

Cách không sử dụng các câu lệnh
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5

Hãy nhớ rằng, so sánh trong Python đánh giá thành

def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
7 hoặc
>>> b = False
>>> if not b:
...     print("Negation in action!")
...
Negation in action!
0. Với các tuyên bố có điều kiện, chúng tôi kiểm tra giá trị đó ngầm. Trong Python, chúng tôi không muốn so sánh với
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
7 hoặc
>>> b = False
>>> if not b:
...     print("Negation in action!")
...
Negation in action!
0 với
>>> message = "Hi there."

>>> a = 0
>>> if a:   # 0 is False-y
...     print(message)
...

>>> b = -1
>>> if b:  # -1 is Truth-y
...     print(message)
...
Hi there.

>>> c = []
>>> if c:  # Empty list is False-y
...     print(message)
...

>>> d = [1, 2, 3]
>>> if d:  # List with items is Truth-y
...     print(message)
...
Hi there.
8.do not want to compare to
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
7 or
>>> b = False
>>> if not b:
...     print("Negation in action!")
...
Negation in action!
0 with
>>> message = "Hi there."

>>> a = 0
>>> if a:   # 0 is False-y
...     print(message)
...

>>> b = -1
>>> if b:  # -1 is Truth-y
...     print(message)
...
Hi there.

>>> c = []
>>> if c:  # Empty list is False-y
...     print(message)
...

>>> d = [1, 2, 3]
>>> if d:  # List with items is Truth-y
...     print(message)
...
Hi there.
8.

CẢNH BÁO - Hãy chú ý, bởi vì mã bên dưới cho thấy những gì bạn không nên làm.shouldn’t do.

# Warning: Don't do this!
>>> if (3 < 5) == True: # Warning: Don't do this!
...     print("Hello")
...
Hello

# Warning: Don't do this!
>>> if (3 < 5) is True: # Warning: Don't do this!
...     print("Hello")
...
Hello

Thay vào đó làm điều này:

>>> if 3 < 5:
...     print("Hello")
...
Hello

Nếu chúng tôi muốn kiểm tra rõ ràng liệu giá trị được đặt rõ ràng thành

def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
7 hoặc
>>> b = False
>>> if not b:
...     print("Negation in action!")
...
Negation in action!
0, chúng tôi có thể sử dụng từ khóa
>>> def modify_name(name):
...    if len(name) < 5:
...         return name.upper()
...    else:
...         return name.lower()
...
>>> name = "Nina"
>>> modify_name(name)
'NINA'
1.

>>> a = True        # a is set to True
>>> b = [1, 2, 3]   # b is a list with items, is "truthy"
>>>
>>> if a and b:     # this is True, a is True, b is "truthy"
...     print("Hello")
...
Hello
>>> if a is True:   # we can explicitly check if a is True
...     print("Hello")
...
Hello
>>> if b is True:   # b does not contain the actual value of True.
...     print("Hello")
...
>>>

>>> def modify_name(name): ... if len(name) < 5: ... return name.upper() ... else: ... return name.lower() ... >>> name = "Nina" >>> modify_name(name) 'NINA' 2

Câu lệnh

>>> def modify_name(name):
...    if len(name) < 5:
...         return name.upper()
...    else:
...         return name.lower()
...
>>> name = "Nina"
>>> modify_name(name)
'NINA'
2 là những gì bạn muốn chạy nếu và chỉ khi câu lệnh
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5 của bạn được kích hoạt.if and only if your
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5 statement wasn’t triggered.

Một tuyên bố

>>> def modify_name(name):
...    if len(name) < 5:
...         return name.upper()
...    else:
...         return name.lower()
...
>>> name = "Nina"
>>> modify_name(name)
'NINA'
2 là một phần của tuyên bố
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5. Nếu câu lệnh
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5 của bạn chạy, câu lệnh
>>> def modify_name(name):
...    if len(name) < 5:
...         return name.upper()
...    else:
...         return name.lower()
...
>>> name = "Nina"
>>> modify_name(name)
'NINA'
2 của bạn sẽ không bao giờ chạy.

def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
0

Và ngược lại.

def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
1

Trong bản sao, nó phải được viết trên dòng sau dòng mã thụt cuối cùng của bạn. Trong mã Python trong một tệp, có thể có bất kỳ mã nào khác giữa

def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5 và
>>> def modify_name(name):
...    if len(name) < 5:
...         return name.upper()
...    else:
...         return name.lower()
...
>>> name = "Nina"
>>> modify_name(name)
'NINA'
2.

Bạn sẽ thấy

>>> def num_info(num):
...    if num > 0:
...        print("Greater than zero")
...        if num > 10:
...            print("Also greater than 10.")
...
>>> num_info(1)
Greater than zero
>>> num_info(15)
Greater than zero
Also greater than 10.
1 nếu bạn cố gắng tự viết một câu lệnh
>>> def modify_name(name):
...    if len(name) < 5:
...         return name.upper()
...    else:
...         return name.lower()
...
>>> name = "Nina"
>>> modify_name(name)
'NINA'
2 hoặc đặt thêm mã giữa
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5 và
>>> def modify_name(name):
...    if len(name) < 5:
...         return name.upper()
...    else:
...         return name.lower()
...
>>> name = "Nina"
>>> modify_name(name)
'NINA'
2 vào tệp Python.

def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
2

>>> def num_info(num): ... if num > 0: ... print("Greater than zero") ... if num > 10: ... print("Also greater than 10.") ... >>> num_info(1) Greater than zero >>> num_info(15) Greater than zero Also greater than 10. 5 có nghĩa là khác, nếu.

>>> def num_info(num):
...    if num > 0:
...        print("Greater than zero")
...        if num > 10:
...            print("Also greater than 10.")
...
>>> num_info(1)
Greater than zero
>>> num_info(15)
Greater than zero
Also greater than 10.
5 có nghĩa là khác nếu. Điều đó có nghĩa là, nếu tuyên bố
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5 này không được coi là
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
7, hãy thử thay thế.if this
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
5 statement isn’t considered
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
7, try this instead.

Bạn có thể có nhiều câu lệnh

>>> def num_info(num):
...    if num > 0:
...        print("Greater than zero")
...        if num > 10:
...            print("Also greater than 10.")
...
>>> num_info(1)
Greater than zero
>>> num_info(15)
Greater than zero
Also greater than 10.
5 trong mã của mình như bạn muốn. Họ được đánh giá theo thứ tự mà họ đã tuyên bố cho đến khi Python tìm thấy một thứ mà
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
7. Điều đó chạy mã được xác định trong
>>> def num_info(num):
...    if num > 0:
...        print("Greater than zero")
...        if num > 10:
...            print("Also greater than 10.")
...
>>> num_info(1)
Greater than zero
>>> num_info(15)
Greater than zero
Also greater than 10.
5 đó và bỏ qua phần còn lại.until Python finds one that’s
def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
7. That runs the code defined in that
>>> def num_info(num):
...    if num > 0:
...        print("Greater than zero")
...        if num > 10:
...            print("Also greater than 10.")
...
>>> num_info(1)
Greater than zero
>>> num_info(15)
Greater than zero
Also greater than 10.
5, and skips the rest.

def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules
3

Tại sao Python cứ nói cú pháp không hợp lệ?

Lỗi cú pháp được tạo bởi Python khi nó đang dịch mã nguồn thành mã byte.Họ thường chỉ ra rằng có điều gì đó không ổn với cú pháp của chương trình.Ví dụ: Bỏ qua dấu hai chấm ở cuối câu lệnh DEF mang lại cú pháp tin nhắn có phần dư thừa: cú pháp không hợp lệ.

Tại sao tôi gặp lỗi với người khác trong Python?

Nếu bạn đang gặp lỗi về cách khác vì bạn đã nói với thông dịch viên rằng;là kết thúc của câu lệnh IF của bạn, vì vậy khi nó tìm thấy một vài dòng sau đó, nó bắt đầu phàn nàn.you've told the interpreter that the ; was the end of your if statement so when it finds the else a few lines later it starts complaining.

Làm thế nào để bạn giải quyết cú pháp không hợp lệ Elif trong Python?

ELIF của bạn không được thụt vào đúng cách ... Nó nên được thụt vào theo cách tương tự nếu được thụt vào.Nhìn thấy khối khác, có vẻ như bạn có nhầm lẫn với cái đầu tiên nếu.Hãy nhớ rằng elif/khác nên được đi trước bởi một nếu luôn luôn....it should be indented the same way if is indented. Seeing the else block, it seems that you have by mistake indented the first if . Remember that elif/else should be preceded by an if always.