Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Beginner's syntax error

syntax syntax error

  • Please log in to reply
4 replies to this topic

#1 pebz5

pebz5

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 27 June 2011 - 04:20 PM

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


  • 0

#2 Roger

Roger

    Skadoosh!

  • Administrator
  • 1222 posts
  • Programming Language:C, PHP
  • Learning:Others

Posted 27 June 2011 - 05:07 PM

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??"
  • 0

New around here? Click here to register and start participating in under a minute?

Or do a quick search and you may find the answer you're looking for.


#3 pebz5

pebz5

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 27 June 2011 - 05:42 PM

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.
  • 0

#4 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 27 June 2011 - 07:49 PM

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

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#5 pebz5

pebz5

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 28 June 2011 - 05:23 AM

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.
  • 0





Also tagged with one or more of these keywords: syntax, syntax error

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download