2016-04-18 51 views
-6

是否可以使列表中的項目一次顯示一個項目,但是在程序顯示下一個項目之前它必須等待用戶輸入?如何使列表中的項目顯示,並且來自用戶的輸入

這裏是我的代碼:

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

我試圖在重走funcation列表。

+1

是的,這是可能的 – miradulo

+1

當然是這樣,你有沒有嘗試過任何東西? –

+0

我累了,但我不能找到正確的編碼。如果你能以一個很棒的例子展示我。 – Jamal

回答

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->Ç

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

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

A)第一個答案
B)第二應答
C)最後的回答

每一個seperatedly。

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

生成經過格式化的回答字符串(迭代的列表,格式的字符串,並將其轉儲到一個列表。
現在,

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

注意到該清單,並與\n串接它s。現在剩下的一切就是打印它並等待一個答案
希望我幫助

+0

你正在使用哪個python版本? – Jamal

+0

2.7.3 您使用的是什麼版本? (我可以優化你的版本的解決方案) –

+0

3.5.1當我看到原始輸入我沒有認出它 – Jamal

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 
+0

謝謝你的迴應。 – Jamal

+0

有人可以解釋downvote?這只是說明方法的代碼片段。 – trans1st0r

+0

我想他的意思是所有的答案都會顯示出來,只有raw_input部分 –

相關問題