【问题标题】:EOF while parsing in python在python中解析时的EOF
【发布时间】:2021-05-16 10:40:24
【问题描述】:

我正在尝试创建一个打印随机字母的游戏,并且用户必须从该列表中创建一个单词。用户可以选择退出、查看分数和继续玩。我很难弄清楚我的代码有什么问题。错误说“语法错误:解析时出现意外的EOF”

到目前为止,我已经想出了这段代码。 这是我的代码:

class Node:
    def __init__(self, key):
        self.left = None
        self.right = None
        self.val = key

def printPreorder(root):
     if root:
        print(root.val),
        printPreorder(root.left)
        printPreorder(root.right)


class stack:
     def __init__(self):
        self.items = []

    def push(self, item): 
        return self.items.append(item)
    
    def pop(self):
        return self.items.pop()

    def size(self):
        return len(self.items)

print ("Create 3 words using the letters")
root = Node('a')
root.right = Node('n')
root.left = Node ('t')
printPreorder(root)
print("-----------------------------------")

s1 = stack()
while (s1.size() <3):
    try:
        FirstList = (input("Enter the word you created: "))
        if (FirstList == 'an'):
            s1.push(FirstList)

        elif (FirstList == 'ant'):
            s1.push(FirstList)

        elif (FirstList == 'tan'):
            s1.push(FirstList)

    finally:
        print(s1.items)

 legend1 = (input("Choose next move 1 = Countinue to the next round 2 = Show score3 = Exit Legend: "))
    if legend1 == '3':
        exit1 = (input("Are you sure you want to exit? 1 = yes 2 = no"))
        if (exit1 == '1'): 
            break
        elif (exit1 == '2'):
            return
        else:
            print("Enter valid legend: ")

    elif legend1 == '2':
        print(s1.size())

    elif legend1 == '1':
        s2 = stack()
        print ("Create 3 words using the letters")
        root = Node('i')
        root.right = Node('d')
        root.left = Node ('m')
        printPreorder(root)
        print("-----------------------------------")
        s1 = stack()
        while (s2.size() <3):
            try:
                SecondList = (input("Enter the word you created: "))
                if (SecondList == 'hid'):
                    s2.push(SecondList)

                elif (SecondList == 'dim'):
                    s2.push(SecondList)

                elif (SecondList == 'id'):
                    s2.push(SecondList)

            finally:
                print(s2.items)

请帮我修正语法错误。

【问题讨论】:

  • 修正缩进。
  • 由于其他错误(例如缩进)以及在函数外使用return,您的错误无法重现。您能否修复这些错误,或者提供一个更简单的玩具示例?

标签: python data-structures stack syntax-error binary-search-tree


【解决方案1】:

这应该现在运行。 您不能在函数和循环之外使用returnbreak。我想你想要的是exit()

您可以通过使用为编码而设计的文本编辑器(例如 VS Code)来避免 EoF 问题。他们会告诉你这些错误的位置。

最后,您可以在字符串中使用\n 来添加换行符。这将使您的print 输出更具可读性。

class Node:
    def __init__(self, key):
        self.left = None
        self.right = None
        self.val = key

def printPreorder(root):
    if root:
        print(root.val),
        printPreorder(root.left)
        printPreorder(root.right)


class stack:
    def __init__(self):
        self.items = []

    def push(self, item): 
        return self.items.append(item)
    
    def pop(self):
        return self.items.pop()

    def size(self):
        return len(self.items)

print ("Create 3 words using the letters")
root = Node('a')
root.right = Node('n')
root.left = Node ('t')
printPreorder(root)
print("-----------------------------------")

s1 = stack()
while (s1.size() <3):
    try:
        FirstList = (input("Enter the word you created: "))
        if (FirstList == 'an'):
            s1.push(FirstList)

        elif (FirstList == 'ant'):
            s1.push(FirstList)

        elif (FirstList == 'tan'):
            s1.push(FirstList)

    finally:
        print(s1.items)

legend1 = (input("Choose next move\n1 = Countinue to the next round\n2 = Show score\n3 = Exit\nLegend: "))
if legend1 == '3':
    exit1 = (input("Are you sure you want to exit?\n1 = yes\n2 = no"))
    if (exit1 == '1'): 
        exit()
    elif (exit1 == '2'):
        exit()
    else:
        print("Enter valid legend: ")

elif legend1 == '2':
    print(s1.size())

elif legend1 == '1':
    s2 = stack()
    print ("Create 3 words using the letters")
    root = Node('i')
    root.right = Node('d')
    root.left = Node ('m')
    printPreorder(root)
    print("-----------------------------------")
    s1 = stack()
    while (s2.size() <3):
        try:
            SecondList = (input("Enter the word you created: "))
            if (SecondList == 'hid'):
                s2.push(SecondList)

            elif (SecondList == 'dim'):
                s2.push(SecondList)
            
            elif (SecondList == 'id'):
                s2.push(SecondList)
            
        finally:
            print(s2.items)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    相关资源
    最近更新 更多