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
什麼格式的文件嗎? – rassar
檢查用戶輸入和答案周圍的其他空白處。如果您認爲合適,請使用'.strip()'。出於調試目的,打印每個循環中的所有變量,或使用pdb進行更多交互式調試。 – bheklilr
@rassar我的文件是.txt格式。 – singha4086