Jump to content

Homework help, getting syntax error.

Go to solution Solved by elpiop,

you are missing two closing parentheses in your print statement. it should be:

 

print(str(index+1) + "\t" + correct_answer[index].append(input("please enter your answer: ")))

 

Hello guys!

 

First I am extremely new to Python as well as this is my first time programming so forgive me!

 

I am just at a lost on what I am doing wrong and would like to find out why I am getting a syntax error, here is my program code:

 

Quote

def main():
    correct_answer = ["A", "C", "A", "A", "D", "B",
                     "C", "A", "C", "B", "A", "D",
                     "C", "A", "D", "C", "B", "B",
                     "D", "A"]
    user_answer = []
    correct_count = 0
    incorrect_count = 0
    numner_of_questions = 20

    index = 0

    print("Q\tocrr\tYour\tStatus")
    print("#\tAnswer\tAnswer\n--------------------------")

    while index < 20:
        print(str(index+1) + "\t" + correct_answer[index].append(input("please enter your answer: ")
            if user_answer[index].strip() == correct_answer[index]:
                correct_count += 1
                index += 1
                print("         Correct")
            else:
                incorrect_count += 1
                index += 1
                print("         Incorrect")

    percent_correct = (correct_count/number_of_questions) * 100
    percent_correct_fmt = format(percent_correct, ".lf")
    print("Grade: ", correct_count , "/", number_of_questions, " = ",
          percent_correct_fmt, sep="")

    if percent_correct >= 75:
        print("congratulations, you have passed the exam!")
    else:
        print("Unfortunately, you have not passed the exam..")


main()

The error code I am getting is on this line:

  • if user_answer[index].strip() == correct_answer[index]: <--

which the semicolon is where I am getting the error. Any insight on what I am missing? Thank you in advance!

 

 

Link to comment
https://linustechtips.com/topic/774172-homework-help-getting-syntax-error/
Share on other sites

Link to post
Share on other sites

You're missing the ending bracket after index.strip()

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to post
Share on other sites

18 minutes ago, rizenfrmtheashes said:

yup.  you're missing a close bracket `]` after index.strip()

 

32 minutes ago, fizzlesticks said:

You're missing a ']'

 

32 minutes ago, djdwosk97 said:

You're missing the ending bracket after index.strip()

 

Sorry I typo'd there! The quote I made was the exact one I, the program does have the [index].strip quote.

Link to post
Share on other sites

7 hours ago, elpiop said:

you are missing two closing parentheses in your print statement. it should be:

 


print(str(index+1) + "\t" + correct_answer[index].append(input("please enter your answer: ")))

 

Yes that solved that issue! I am now getting a traceback error but I will at least attempt to figure that part out before asking again. Thanks a bunch!

Link to post
Share on other sites

seeing as you know how many loops you're doing you should be using a for loop.

 

for index in range(number_of_questions):

 

so you wouldn't need to manually add to index or set it to 0 at the top. 

 

also there is no point holding the users answer in an array as you're keeping a count of correct answers, unless you're planning to show answers back to the user at a later point. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×