0
votes

I'm trying to create a game that prints shuffled letters and the user has to create a word/s from that list. Users have options to exit, view scores, and continue playing. I'm having a hard time trying to figure out what's wrong with my code. The error said "Syntax error: unexpected EOF while parsing"

So far I've come up with this code. Here is my code:

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)

Please help me fix the syntax error.

1
Fix your indentation. - rdas
Your error is unreproducible due to other errors such as indentation, and using return outside of a function. Could you either fix these errors, or provide a simpler toy example? - Lucas Ng

1 Answers

0
votes

This should run now. You cannot use return or break outside of functions and loops. I think what you want is exit()

You can avoid EoF problems by using text editors made for coding such as VS Code. They will show you where these errors are located.

Lastly, you can use \n in strings to add newlines. This will make your print output more readable.

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)