0

I am having some trouble with exercise 'Knights' (week 1 - 2020 CS50ai). https://cs50.harvard.edu/ai/2020/projects/1/knights/

The goal of this exercise is to write a program to solve logic puzzels. The objective of the puzzle is, given a set of sentences spoken by each of the characters, determine, for each character, whether that character is a knight (tells the truth) or a knave (lies).

I wrote the code below related to this assignment, however when I run the code, I get the error:

def main(): ^ SyntaxError: invalid syntax

Since this is the part that is created by CS50ai and I should not adapt anything there, I have know idea what to do to get the code runned. Hopefully someone can help me with this problem. Thank you in advance!

Code:

from logic import *

AKnight = Symbol("A is a Knight")
AKnave = Symbol("A is a Knave")

BKnight = Symbol("B is a Knight")
BKnave = Symbol("B is a Knave")

CKnight = Symbol("C is a Knight")
CKnave = Symbol("C is a Knave")

# Puzzle 0
# A says "I am both a knight and a knave."
knowledge0 = And(
    Or(AKnight,AKnave),
    Not(And(AKnight,AKnave)),

    # If A is a knight -> A is a knight and a knave
    Implication(AKnight, And(AKnight, AKnave)),
    # If A is a knave -> A is not a knight and a knave
    Implication(AKnave, Not(And(AKnight, AKnave))),
)


# Puzzle 1
# A says "We are both knaves."
# B says nothing.
knowledge1 = And(
    Or(AKnight,AKnave),
    Not(And(AKnight,AKnave)),

    Or(BKnight,BKnave),
    Not(And(BKnight,BKnave)),

    # If A is knight -> A and B are both knaves
    Implication(AKnight, And(AKnave, BKnave)),
    # If A is a knave -> A is a knave and B is a knight (not both a knave)
    Implication(AKnave, Not(And(AKnave, BKnave))),
)

# Puzzle 2
# A says "We are the same kind."
# B says "We are of different kinds."
knowledge2 = And(
    Or(AKnight,AKnave),
    Not(And(AKnight,AKnave)),

    Or(BKnight,BKnave),
    Not(And(BKnight,BKnave)),

    #If A is a knight -> B is also a knight and the other way around (for completion also check for A is knave and B is knave that AI know this is wrong)
    Implication(AKnight, Or(Biconditional(AKnight, BKnight), Biconditional(AKnave, BKnave))),
    #If A is knave -> B is not a knave
    Implication(AKnave, Not(Or(Biconditional(AKnight, BKnight), Biconditional(AKnave, BKnave)))),

    #If 
    Implication(BKnight, Or(Biconditional(AKnight, BKnave), Biconditional(AKnave, BKnight))),
    Implication(BKnight, Not(Or(Biconditional(AKnight, BKnave), Biconditional(AKnave, BKnight)))),   
)


# Puzzle 3
# A says either "I am a knight." or "I am a knave.", but you don't know which.
# B says "A said 'I am a knave'."
# B says "C is a knave."
# C says "A is a knight."
knowledge3 = And(
    Or(AKnight,Aknave),
    Not(And(AKnight,AKnave)),

    Or(BKnight,Bknave),
    Not(And(BKnight,BKnave)), 

    Or(CKnight,Cknave),
    Not(And(CKnight,CKnave)),  

    #3.1 If A is a Knight -> 
    Implication(AKnight,Or(AKnight, Aknave)),
    Implication(AKnave, Not(Or(AKnight, AKnave)),

    #3.2 If B is a knight -> A is ka
    Implication(Bknight, implication(AKnight, AKnave)),
    Implication(BKnave, Not(implication(AKnight, AKnave))),

    #3.3 If B is a knight -> C is a knave 
    Implication(BKnight, CKnave),
    Implication(BKnave, Not(CKnave)),

    #3.4 If C is a knight -> A is a knight
    Implication(CKnight, AKnight),
    Implication(CKnave, Not(AKnight)),
)


def main():
    symbols = [AKnight, AKnave, BKnight, BKnave, CKnight, CKnave]
    puzzles = [
        ("Puzzle 0", knowledge0),
        ("Puzzle 1", knowledge1),
        ("Puzzle 2", knowledge2),
        ("Puzzle 3", knowledge3)
    ]
    for puzzle, knowledge in puzzles:
        print(puzzle)
        if len(knowledge.conjuncts) == 0:
            print("    Not yet implemented.")
        else:
            for symbol in symbols:
                if model_check(knowledge, symbol):
                    print(f"    {symbol}")


if __name__ == "__main__":
    main()
2
  • The error is detected at that line, but comes before the def main(): . knowledge3 block has unbalanced parens. – DinoCoderSaurus Aug 18 '20 at 14:13
  • Thank you for the help. Now it works fine! – KP.cs50ai Aug 22 '20 at 17:45

You must log in to answer this question.

Browse other questions tagged .