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)
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])
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.
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 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.