如何使用用户的输入显示列表中的项目

时间:2016-04-18 19:52:24

标签: python

是否可以让列表中的项目一次显示一个,但是在程序显示下一个项目之前必须等待用户输入?

这是我的代码:

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()

我尝试了重新拍摄功能列表。

2 个答案:

答案 0 :(得分:0)

见:

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

答案 1 :(得分:0)

你可以这样做:

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

编辑(适用于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))]))

<强> 说明:

for x in xrange(len(a))

遍历列表a

的索引
chr(x + ord('A'))

将索引转换为字母 - 0-> A,1-> B,2-> C

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

格式化包含答案信和答案本身的字符串。

  

A)第一个回答
  B)第二个回答
  C)最后答案

每个人分开。

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

生成格式化答案字符串列表(迭代a,格式化字符串,并将其转储到列表中。
现在,

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

获取该列表,并将其与\n连接。现在剩下的就是打印它并等待答案。 希望我帮忙。