我正在尝试在python中进行测验,在那里我使用了很多while循环,所以我可以轻松地保留代码,或者重新运行它。问题是,一旦我的一个嵌套循环完成运行,代码就不会继续运行。我会留下一些伪代码,因为我的逻辑不正确,以及之后的实际代码。
Pseudocode
i = 0
array = [0]
while i < 1:
while length of array < 11:
do something
print "done something!"
基本上,一旦阵列的长度达到11,就不会发生打印。 这是实际的代码
diff =int(input("Choose a difficulty; 1 2 3"))
diffs = [1, 2, 3]
while diff not in diffs:
diff = int(input("Invalid input; choose a difficulty; 1 2 3"))
data = []#This will hold all of the data in the quiz file
answers = []#This will hold the current answers available to the user
answers2 = []#This will hold the current questions used, so that the answers do appear in a random order
letters = ["A. ","B. ","C. ","D. "]#This is so that each answer has an identifier
Questions = [0]
i = 0
score = 0
with open(filenameq,"r") as quizFile:
fileReader = csv.reader(quizFile)
for row in fileReader:
data.append(row)#This creates a 2D array, so that the program can access specific values in the file
while i < 1:
while len(Questions) < 11:
a = 0
while a in Questions:
a = randint(0,9)
Questions.append(a)
print(data[0][a])#The cell where the question is located in the file
answers = []
answers2 = []
for x in range(0,(diff+1)):
answers.append(data[a+1][x])
x = 0
b = 0
correct = 0
while x <= diff:
b = randint(0,diff)
if b in answers2:
continue
answers2.append(b)
print(letters[x]+answers[b])
if b == 0:#the first item in the CSV file is the correct value
correct = x
x += 1
answer = input("Enter the letter of the answer you think is correct").upper()
if correct == letters.index(str(answer[0]+". ")):#This is the index of the letter that the user entered, in the letters list
score += 1
print("Quiz finished")
with open(filename,"a+") as scoreFile:
fileWriter = csv.writer(scoreFile)
fileReader = csv.reader(scoreFile)
for row in fileReader:
if row[0] == username:
print(row)
row[2] = "y"
print(row)
fileWriter.writerow(row)
最后,这是我试图操作的csv文件
What is the name of the gorgon in Greek Mythology?,How did a warrior defeat Medusa in Greek Mythology,Who is the God of the Yellow River in Chinese Mythology?,Who is the mother of Hephaestus in Greek Mythology?,Who is the mother of Hephaestus in Greek Mythology?,Which river was Achilles dipped in as a baby?,Where did Zeus cast the Titans after Cronus defeated them?,What does the Helm of Darkness grant to the wearer?,Which is a pantheon of Norse Gods - excluding the Aesir?,What is Yggdrasil?
Perseus,Medusa,Ares,Zeus
A Shield,A Virus,Laceration,Cavalry
He Bo,Yang Zing,Kukulkan Ah Puch
Hera,Aphrodite,Demeter,Persephone
Pomegranate,Orange,Guava,Apple
Styx,Cocytus,Acheron,Phlegethon
Tartarus,The Asphodel Meadows,The Underworld,The Mourning Fields
Invisibility,Invincibility,Immortality,Ignitability
Vanir,Hel,Tyr,Yggdrasil
A Plant,A Person,A Deity,A World
因此,每个问题都位于顶部,每个问题的答案都在底部,正确答案为行[0],或每行中的第一个索引。
提前感谢您帮助我:)
编辑:在我的主代码中添加了一些额外的代码,我忘了最初包括,以澄清“差异”和“差异”是什么
答案 0 :(得分:0)
似乎是因为你永远不会关闭你的初始while循环:while i < 1
。由于i的值保持为0,因此最外面的while循环将永远不会关闭,导致程序陷入无限循环。如果您通过在结尾设置i = 1
来关闭该循环,则应解决此特定问题。
答案 1 :(得分:0)
好吧,我自己想出来了,这正是我的想法;第一个嵌套while循环中的逻辑阻止它结束,因为Questions数组为Questions = [0]
,如果a
已经Questions
,则a
变量无法进入Questions
在a
,但0
已经在Questions
中,defaultProps
可能已经是0-9。基本上,它永远不会结束!