Jump to content

Beginner's syntax error

- - - - -

  • Please log in to reply
4 replies to this topic

#1
pebz5

pebz5

    Newbie

  • Members
  • Pip
  • 5 posts
Hi everyone,
I'm a new poster here, and a complete and total beginner at programming. I decided to start with Python since I'd read that it was an easy one to learn for beginners. I'm using some online tutorials, and one of the exercises was to work on some code for a small quiz program. However, I keep getting a strange syntax error, and I can't get the reason. I was hoping that someone experienced could take a look and tell me what I'm missing? Thanks a lot.

def main_menu():

    print("1. Print the menu")

    print("2. Take the test")

    print("3. Quit the program:")

def get_questions():

    return[["What color is the daytime sky on a clear day? ", "blue"],

            ["What is the answer to life, the universe and everything? ", "42"],

            ["What is a three letter word for mouse trap? ", "cat"],

            ["What noise does a truly advanced machine make?", "ping"]]

def check_question(question_and_answer):

        question = question_and_answer[0]

        answer = question_and_answer[1]

        given_answer = input(question)

        if answer == given_answer:

            print("Correct")

            return True

        else:

            print("Incorrect, correct was:", answer)

            return False


def run_test(questions):

    if len(questions) == 0:

        print("No questions were given.")

        return

    index = 0

    right = 0

    while index < len(questions):

        if check_question(questions[index]):

            right = right + 1

        index = index + 1

    print("You got", right " 100 / len(questions),\

        % right out of", len(questions))

choice = 1

while choice != 3:

    if choice == 1: #Here's where I get a syntax error, in a small space right before the if.

        main_menu()   

    elif choice == 2:

        run_test(questions)

    print()

    choice = int(input("Choose an option: "))

print("Goodbye")




#2
Roger

Roger

    If nothing goes right, go left.

  • Administrators
  • 718 posts
  • Programming Language:C, PHP
  • Learning:Python
Hi pebz5, I'm not a Python programmer, but I'm sure someone who can help is going to ask you "what errors are you seeing??"
Check out our update Guidelines/FAQ. When posting code, remember to use code tags - Posted Image.

#3
pebz5

pebz5

    Newbie

  • Members
  • Pip
  • 5 posts
Thanks for replying,
Sorry about that, I'm new. I left a comment in the code where I was hitting the problem so that anyone looking at the code would see it, but to clarify, it's towards the bottom right before the end in the section that designates what each of the options should do. It's on the line if choice == 1:. Thanks again.

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts

pebz5 said:

if choice == 1: #Here's where I get a syntax error, in a small space right before the if.
That's odd, because the real problem is here:
    print("You got", right " 100 / len(questions),\

        % right out of", len(questions))
You neglected to put a comma between right and the next quotation.
    print("You got", right, " 100 / len(questions),\

        % right out of", len(questions))
While that fixes the syntax error, that probably still won't do what you want. Probably something more like this...
    print("You got ", float(right) / len(questions),

          "% right out of ", len(questions))

Wow I changed my sig!

#5
pebz5

pebz5

    Newbie

  • Members
  • Pip
  • 5 posts
Thanks a lot. Yeah, I don't know why, but for some reason IDLE was marking the error in a different spot. But sure enough, when I fixed the error you saw the program loaded without any trouble. The code I wound up using went

print("You got", right, right / len(questions) * 100,

    "% right out of", index)

This seems to work. Thanks for spotting that error, I had completely overlooked it. The code's working now.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users