2

I am trying to format output of an index and list from this:

[6]
[7]
[8]
[9]
The answers should be: 
['b', 'c', 'a', 'c']`

To more like this:

[6]  b   
[7]  c
[8]  a    
[9]  c

Here is the code snippet:

print( "Here are the questions the you got wrong: ")
for i in range (0, 10):
    if q[i] != answers[i]:
        print ( [i+1],)
    else:
        ("You got all of the questions correct, Good Job. ")
print("The answers should be: ")
print(wrongList)

3 Answers 3

2

You didn't collect the wrongList but it'll be easy to do with:

print("The answers should be: ")
for i in range (0, 10):
    if q[i] != answers[i]:
        print (q[i], answers[i])
Sign up to request clarification or add additional context in comments.

Comments

1

Just print answers[i]:

for i in range (10):
    print("[{}] {}".format(i+1, answers[i])

If there are only ten answers you can use enumerate with a starting value of 1, enumerate(answers, start=1) or if there are more the than ten you will have to slice it:

 for i  answer in enumerate(answers[:10], start=1)

You can also zip and forget indexing:

for i, (a, b) in enumerate(zip(answers, p),1):
    if a != b:
       print("[{}] {}".format(count, a))
    else:
       print("You got all of the questions correct, Good Job. ")

"[{}] {}".format(count, a) is using str.format which is generally the preferred method.

Comments

1

You can use enumerate to loop through a list and get the index:

for i, answer in enumerate(answers):
    print("[%s] %s" % (i, answer))

If the print statement doesn't make sense, take a look at the documentation for string formatting.

2 Comments

The enumeration starts at 6, no?
@MalikBrahimi The question doesn't make it clear. It sounds like there are 10 questions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.