-6

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.

Jamal
  • 1
  • 4

2 Answers2

0

See this:

l = [1, 2, 3]  # Your list
for i in l:
 input = raw_input("Show message") # Get input before
 print "current list item ", i     # displaying list item
trans1st0r
  • 2,023
  • 2
  • 17
  • 23
0

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 range(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.

Yotam Salmon
  • 2,400
  • 22
  • 36
  • Which python version are you using? – Jamal Apr 18 '16 at 20:24
  • 2.7.3 What version are you using? (I can optimize the solution for your version) – Yotam Salmon Apr 18 '16 at 20:25
  • 3.5.1 when i saw raw input i didnt recognoise it – Jamal Apr 18 '16 at 20:26
  • So raw_input() should be change to input() – Yotam Salmon Apr 18 '16 at 20:29
  • I have eddited my answer to match also Python 3. If it helped you, please upvote it or tick it as the correct answer. – Yotam Salmon Apr 18 '16 at 20:30
  • By the way, when you used the For loop, i noticed you added x to range. Was that a typo? – Jamal Apr 18 '16 at 20:36
  • No, indeed. The range method generates a list of numbers (example: [0, 1, 2, 3, 4, 5]. The xrange method just simulates that list without really creating it in the memory. This allows us to iterate through huge lists without using a lot of memory. In this particular case, you can use both range and xrange, but in other cases xrange is neccesarry. FYI: http://stackoverflow.com/questions/94935/what-is-the-difference-between-range-and-xrange-functions-in-python-2-x. In Python 3, AFAIK, they dropped range, and changed Python 2's xrange to Python 3's range. So in Python 3 it's range. – Yotam Salmon Apr 18 '16 at 20:48
  • 'range() now behaves like xrange() used to behave, except it works with values of arbitrary size' i read this in the python website. They introduced this after the 3.5 version. That is why my IDLE didn't register xrange. – Jamal Apr 18 '16 at 20:55
  • Yea, Forgive me for the lack of knowledge with Python 3.x, I just really like 2.7 and updating is currently not an option... – Yotam Salmon Apr 18 '16 at 20:58
  • No problem man, i appreciate you helping me. – Jamal Apr 18 '16 at 20:59