Jamal Jamal - 7 months ago 8
Python Question

How to make items on a list show, with input from the user

Is it possible to make items on a list appear one at a time but it has to wait for the user to input, before the program shows the next item?

here is my code:

import os

correct=0
incorrect=0
missed_questions=[]
incorrect_questions=[]

def question1(question_name,question):
print(question_name)
print(question)
answer=int(input())
if answer == 4:
os.system('cls')
global correct
correct=correct+1
elif answer != 4:
global incorrect
incorrect=incorrect+1
global missed_questions
missed_questions.append(question_name)
global incorrect_questions
incorrect_questions.append(question)
def question2(question_name,question):
print(question_name)
print(question)
answer=int(input())
if answer == 1:
os.system('cls')
global correct
correct=correct+1
elif answer != 1:
global incorrect
incorrect=incorrect+1
global missed_questions
missed_questions.append(question_name)
global incorrect_questions
incorrect_questions.append(question)
def question3(question_name,question):
print(question_name)
print(question)
answer=int(input())
if answer == 2:
os.system('cls')
global correct
correct=correct+1
elif answer != 2:
global incorrect
incorrect=incorrect+1
global missed_questions
missed_questions.append(question_name)
global incorrect_questions
incorrect_questions.append(question)
def question4(question_name,question):
print(question_name)
print(question)
answer=int(input())
if answer == 3:
os.system('cls')
global correct
correct=correct+1
elif answer != 3:
global incorrect
incorrect=incorrect+1
global missed_questions
missed_questions.append(question_name)
global incorrect_questions
incorrect_questions.append(question)
def question5(question_name,question):
print(question_name)
print(question)
answer=int(input())
if answer == 3:
os.system('cls')
global correct
correct=correct+1
elif answer != 3:
global incorrect
incorrect=incorrect+1
global missed_questions
missed_questions.append(question_name)
global incorrect_questions
incorrect_questions.append(question)
def question6(question_name,question):
print(question_name)
print(question)
answer=int(input())
if answer == 4:
os.system('cls')
global correct
correct=correct+1
elif answer != 4:
global incorrect
incorrect=incorrect+1
global missed_questions
missed_questions.append(question_name)
global incorrect_questions
incorrect_questions.append(question)
def question7(question_name,question):
print(question_name)
print(question)
answer=int(input())
if answer == 3:
os.system('cls')
global correct
correct=correct+1
elif answer != 3:
global incorrect
incorrect=incorrect+1
global missed_questions
missed_questions.append(question_name)
global incorrect_questions
incorrect_questions.append(question)
def question8(question_name,question):
print(question_name)
print(question)
answer=int(input())
if answer == 1:
os.system('cls')
global correct
correct=correct+1
elif answer != 1:
global incorrect
incorrect=incorrect+1
global missed_questions
missed_questions.append(question_name)
global incorrect_questions
incorrect_questions.append(question)
def question9(question_name,question):
print(question_name)
print(question)
answer=int(input())
if answer == 1:
os.system('cls')
global correct
correct=correct+1
elif answer != 1:
global incorrect
incorrect=incorrect+1
global missed_questions
missed_questions.append(question_name)
global incorrect_questions
incorrect_questions.append(question)
def question10(question_name,question):
print(question_name)
print(question)
answer=int(input())
if answer == 1:
os.system('cls')
global correct
correct=correct+1
elif answer != 1:
global incorrect
incorrect=incorrect+1
global missed_questions
missed_questions.append(question_name)
global incorrect_questions
incorrect_questions.append(question)

def quiz():
question1('Question 1:', '123 - 39 = ? \n 1. 64 \n 2. 44 \n 3. 74 \n 4. 84')
question2('Question 2:' ,'123 + 39 = ? \n \n 1. 162 \n 2. 166 \n 3. 62 \n 4. 66')
question3('Question 3:' ,'123 * 9 = ? \n 1. 1007 \n 2. 1107 \n 3. 1106 \n 4. 1116')
question4('Question 4:' ,'135 / 15 = ? \n 1. 8 \n 2. 8.5 \n 3. 9 \n 4. 9.5')
question5('Question 5:' ,'12 * (12 / 2) = ? \n 1. 144 \n 2. 6 \n 3. 72 \n 4. 36')
question6('Question 6:' ,'130 / 2 + 8 = ? \n 1. 13 \n 2. 14 \n 3. 61 \n 4. 84')
question7('Question 7:' ,'10 + 12 + 13 * 6 / 2 = ? \n 1. 105 \n 2. 44 \n 3. 61 \n 4. 84')
question8('Question 8:' ,'10 + 12 + 13 * 6) / 2 = ? \n 1. 50 \n 2. 44 \n 3. 61 \n 4. 84')
question9('Question 9:' ,'8 (12 + 6 / 3 * 2) - 1 = ? \n 1. 127 \n 2. 109 \n 3. 95 \n 4. 135')
question10('Question 10:' ,'1 / 1 * 1 - 1 + 1 = ? \n 1. 1 \n 2. -1 \n 3. 0 \n 4. -2')


def tryagain():
while True:
answer = input('Do you want to try again?: Press Y for yes and N for no ')
if answer.lower().startswith("y"):
os.system('cls')
print('****** Welceome to the Online Maths Test ********')
print()
print()
print()
print()
print()
print()
print()
os.system("PAUSE")
print(quiz())
elif answer.lower().startswith("n"):
exit()
def re_take():
re_take= input('Do you want to re-take the incorrect quesations? Press Y for y and N for No')
if take.lower().startswith("y"):
print(incorrect_questions)
else:
print(tryagain())

print('****** Welceome to the Online Maths Test ********')
print()
print()
print()
print()
print()
print()
print()
os.system("PAUSE")
quiz()
for q in missed_questions:
print('Incorrect questions were: ', q)
print('')
print('')
print('')
re_take()


i attempted the list on the re-take funcation.

Answer

You can do that:

a = ["first answer", "second answer", "last answer"]
answer = raw_input("\n".join([chr(x + ord('A')) + ") " + a[x] for x in xrange(len(a))]))

Edit (for Python 3):

a = ["first answer", "second answer", "last answer"]
answer = input("\n".join([chr(x + ord('A')) + ") " + a[x] for x in xrange(len(a))]))

Explanation:

for x in xrange(len(a))

Iterates through the indexes of the list a

chr(x + ord('A'))

Converts the indexes into letters - 0->A, 1->B, 2->C

chr(x + ord('A')) + ") " + a[x]

Formats a string containing the answer letter and the answer itself.

A) first answer
B) second answer
C) last answer

Each one seperatedly.

[chr(x + ord('A')) + ") " + a[x] for x in xrange(len(a))]

Generates a list of formatted answer strings (iterates through a, formats a string, and dumps it into a list.
Now,

"\n".join[chr(x + ord('A')) + ") " + a[x] for x in xrange(len(a))]

Takes that list, and concatenates it with \ns. Now everything left is to print it and wait for an answer.
Hope I helped.

Comments