简体   繁体   English

while循环结束后,此后代码不再继续

[英]After a while loop has finished, the code doesn't continue after that

I am trying to make a quiz, in python, where I use quite a few while loops, so I can easily leave code, or re run it. 我正在尝试在python中进行测验,在其中使用了很多while循环,因此我可以轻松地保留代码或重新运行它。 The problem is, once one of my nested loops has finished running, the code doesn't continue to run. 问题是,一旦我的嵌套循环之一完成运行,代码就不会继续运行。 I will leave some pseudocode incase my logic is incorrect, and the actual code after that. 如果我的逻辑不正确,我将留下一些伪代码,之后再加上实际代码。

Pseudocode
i = 0
array = [0]
while i < 1:
    while length of array < 11:
        do something
print "done something!"

Basically, once the length of array has reached 11, the print doesn't happen. 基本上,一旦数组的长度达到11,就不会打印。 Here is the actual code also 这也是实际的代码

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)

Finally, here is the csv file i am trying to manipulate 最后,这是我要处理的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    

So, each question is at the top, and the possible answers are in the bottom for each question, with the correct answers as row[0], or the first index in each line. 因此,每个问题在每个问题的顶部,可能的答案在每个问题的底部,正确的答案为row [0]或每行的第一个索引。

Thank you in advance for helping me :) 预先感谢您对我的帮助:)

EDIT: Added some extra code to my main code, that I forgot to include originally, to clarify what "diff" and "diffs" are 编辑:在我的主代码中添加了一些额外的代码,以至于我最初忘了包含这些代码,以澄清“ diff”和“ diffs”是什么

Seems to be due to the fact that you never close your initial while loop: while i < 1 . 似乎是由于您从未关闭初始的while循环: while i < 1 Since the value of i stays at 0, your outermost while loop will never close, causing your program to be stuck in an infinite loop. 由于i的值保持为0,因此最外面的while循环将永远不会关闭,从而导致程序陷入无限循环。 If you close that loop by setting i = 1 at the end, this particular problem should be resolved. 如果通过在最后设置i = 1关闭该循环,则应解决此特定问题。

Alright, I figured it out myself, and it is exactly what I thought it was; 好吧,我自己弄清楚了,这正是我所想的。 the logic in the 1st nested while loop prevented it from ending, as the Questions array was Questions = [0] , and the a variable was prevented from being in Questions if a was already in Questions , but a could already be 0-9, when 0 was already in Questions . 在第一逻辑嵌套while循环从结束防止它,作为问题阵列是Questions = [0]a从在被被阻止可变Questions如果a在已经Questions ,但a可能已经0-9,当0已经在Questions Basically, It never could end! 基本上,它永远不会结束!

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

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