2017-04-26 70 views
0

我正在嘗試學習python並試圖創建一個簡單的應用程序,我將它設置爲從文本文件中讀取行。第一行是問題,第二行是答案。現在,我可以閱讀這個問題和答案。但是,我將用戶答案與實際答案進行比較的部分,即使輸入的答案正確,它也不會執行循環中的操作。 我的代碼:不處理循環

def populate(): 

print("**************************************************************************************************************") 
f=open("d:\\q.txt") 
questionList=[] 

b = 1 
score=0 
start=0 

for line in f.read().split("\n"): 
    questionList.append(line) 


while b<len(questionList): 
    a = questionList[start] 
    print(a) 
    userinput=input("input user") 
    answer=questionList[b] 
    b = b + 2 
    print(answer) 
    if userinput==answer: 
     score =score+1 
     print(score) 
    else: 
     break 
    start += 2 

我真的很感激任何指導。 我q.txt文件:

1. Set of instructions that you write to tell a computer what to do. 
Program 
2. A language's set of rules. 
Syntax 
3. Describes the programs that operate the computer. 
System Software 
4.To achieve a working program that accomplishes its intended tasks by removing all syntax and logical errors from the program 
Debugging 
5.a program that creates and names computer memory locations and can hold values, and write a series of steps or operations to manipulate those values 
Procedural Program 
6. The named computer memory locations. 
Variables 
7. The style of typing the first initial of an identifier in lowercase and making the initial of the second word uppercase. -- example -- "payRate" 
Camel Casing 
8. Individual operations within a computer program that are often grouped together into logical units. 
Methods 
9. This is an extension of procedural programming in terms of using variables and methods, but it focuses more on objects. 
Object Oriented Programming 
10. A concrete entity that has behaviors and attributes. 
Objects 
+0

什麼格式的文件嗎? – rassar

+0

檢查用戶輸入和答案周圍的其他空白處。如果您認爲合適,請使用'.strip()'。出於調試目的,打印每個循環中的所有變量,或使用pdb進行更多交互式調試。 – bheklilr

+0

@rassar我的文件是.txt格式。 – singha4086

回答

1

您的代碼是:

  • 總是問同樣的問題:questionList[start]
  • 扔掉every
  • 在什麼也沒有答案替換每個空間的價值,因此「系統軟件」變成「SystemSoftware」
  • 未能考慮因素:需要在上使用和answer

這裏有一個更Python實現:

#!/usr/bin/env python3 

from itertools import islice 


# Read in every line and then put alternating lines together in 
# a list like this [ (q, a), (q, a), ... ] 
def get_questions(filename): 
    with open(filename) as f: 
     lines = [line.strip() for line in f] 
    number_of_lines = len(lines) 
    if number_of_lines == 0: 
     raise ValueError('No questions in {}'.format(filename)) 
    if number_of_lines % 2 != 0: 
     raise ValueError('Missing answer for last question in {}'.filename) 
    return list(zip(islice(lines, 0, None, 2), islice(lines, 1, None, 2))) 


def quiz(questions): 
    score = 0 
    for question, answer in questions: 
     user_answer = input('\n{}\n> '.format(question)) 
     if user_answer.lower() == answer.lower(): 
      print('Correct') 
      score += 1 
     else: 
      print('Incorrect: {}'.format(answer)) 
    return score 


questions = get_questions('questions.txt') 
score = quiz(questions) 
num_questions = len(questions) 
print('You scored {}/{}'.format(score, num_questions)) 
+0

這個人太棒了。謝謝。 – singha4086