简体   繁体   English

不处理循环

[英]Doesn't process the loop

I am trying to learn python and trying to create a simple application where I have it set to read lines from the text file. 我正在尝试学习python,并尝试创建一个简单的应用程序,在该应用程序中将其设置为从文本文件中读取行。 The first line is the question and second line is answer. 第一行是问题,第二行是答案。 Now, I am able to read the question and answer. 现在,我能够阅读问答。 But the part where I compare user answer with the actual answer, it doesn't perform the actions in the loop even when the answer entered is correct. 但是我将用户答案与实际答案进行比较的部分,即使输入的答案正确,也不会执行循环中的操作。 My code : 我的代码:

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

I would really appreciate any guidance on this. 我对此表示感谢。 My q.txt file: 我的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

Your code was: 您的代码是:

  • always asking the same question: questionList[start] 总是问同样的问题: questionList[start]
  • throwing away the value of every 抛弃every的价值
  • replacing every space in answers with nothing, so "System Software" becomes "SystemSoftware" 用答案替换答案中的每个空格,因此“系统软件”变为“ SystemSoftware”
  • failing to factor in case: need to use .lower() on userinput and answer . 无法考虑以下情况:需要在userinputanswer上使用.lower()

Here's a more pythonic implementation: 这是一个更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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM