0

For CS50AI Problem 1 (Knights), I am really confused as to what is producing this error.

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)
    Biconditional(AKnight, Not(AKnave))
    Biconditional(AKnave, Not(AKnight))

    Implication(AKnight, And(AKinght, AKnave))
    Implication(AKnave, Not(And(AKnight, AKnave)))
)

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

    Implication(AKnight, And(AKnave, BKnave))
    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)
    Or(BKnight, BKnave)
    Biconditional(AKnight, Not(AKnave))
    Biconditional(BKnight, Not(BKnave))
    Biconditional(AKnave, Not(AKnight))
    Biconditional(BKnave, Not(BKnave))

    Implication(AKnight, Or(And(AKnight, BKnight), And(AKnave, BKnave)))
    Implication(AKnave, Not(Or(And(AKnight, BKnight), And(AKnave, BKnave))))
    Implication(BKnight, Or(Not(And(AKnight, BKnight)), Not(And(AKnave, BKnave))))
    Implication(BKnave, Not(Or(Not(And(AKnight, BKnight)), Not(And(AKnave, BKnave)))))
)

# 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)
    Or(BKnight, BKnave)
    Biconditional(AKnight, Not(AKnave))
    Biconditional(BKnight, Not(BKnave))
    Biconditional(CKnight, Not(CKnave))
    Biconditional(AKnave, Not(AKnight))
    Biconditional(BKnave, Not(BKnave))
    Biconditional(CKnave, Not(CKnave))

    Or(Implication(AKnight, AKnight), Implication(AKnight, AKnave))
    Or(Implication(AKnave, Not(AKnight)), Implication(AKnave, Not(AKnave)))
    Implication(BKnight, Or(Implication(AKnight, AKnave), Implication(AKnave, Not(AKnave)))
    Implication(BKnave, Or(Not(Implication(AKnight, AKnave)), Not(Implication(AKnave, Not(AKnave))))
    Implication(BKnight, CKnave)
    Implication(BKnave, Not(CKnight))
    Implication(CKnight, AKnight)
    Implication(CKnave, Not(AKnave))
)


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()

Error:

python puzzle.py
File "puzzle.py", line 16
    Biconditional(AKnight, Not(AKnave))
                ^
SyntaxError: invalid syntax
1

You have to provide commas to keep separate your logical expressions like

And(Or(AKnight, AKnave), Biconditional(AKnight, Not(AKnave)), Biconditional(AKnave, Not(AKnight)), Implication(AKnight, And(AKinght, AKnave)), Implication(AKnave, Not(And(AKnight, AKnave))) ) This is the mistake in your logic.

0

The And() function takes multiple arguments. So when you do knowledge0 = And(x, y, z, etc) for example, you need to put commas in between x, y, and z, not just a new line.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .