我只需要一些帮助就可以让我的if语句工作

时间:2017-11-12 21:20:36

标签: python-3.x

我正在进行一项可以解决不同困难的测验,并且工作正常。唯一的问题是它像我的代码一样忽略了底部的if语句。即使变量' w' = 9,也就是我回答了9个问题时,它仍然没有打印出它继续循环的语句。

import csv

w = 0
score = 0


global q
with open("Computing.txt", "r") as file:
    reader = csv.reader(file)
    for row in reader:
        dif_c = str(dif) +  " "
        if dif_c + str(q) + ")" in row[0]:
            print (row[0].split(dif_c)[1])
            print (row[1])
            if dif == 1:
                print (row[2])
                input10 = input("Answer 'a' or 'b': ")
            elif dif == 2:
                print(row[2] + "\n" + row[3])
                input10 = input("Answer 'a','b' or 'c': ")
            elif dif == 3:
                print(row[2] + "\n" + row[3] + "\n" + row[4])
                input10 = input("Answer 'a','b','c' or 'd': ")
            if input10 == row[dif + 2]:
                print("Correct")
                score = score + 1
                w = w + 1
            elif input10 != row[dif + 2]:
                print("Incorrect")
                w = w + 1

if w == 9:
    print("Game over")
    print("You got", r, "right out of 10")



while True:
    quiz()

这是所有的测验功能,我定义了w并且在我知道不起作用的函数中得分为0但我不知道如何解决它

1 个答案:

答案 0 :(得分:0)

检查'w == 9'的'if'条件应该在'for'循环内,你需要根据这个条件打破'for'循环。否则它将继续循环。

目前,您的'if'检查位于'for'循环之外。

所以它应该改成这样的东西:

import csv
w = 0 
score = 0
global q
with open("Computing.txt", "r") as file:
    reader = csv.reader(file)
    for row in reader:
        dif_c = str(dif) +  " "
        if dif_c + str(q) + ")" in row[0]:
            print (row[0].split(dif_c)[1])
            print (row[1])
            if dif == 1:
                print (row[2])
                input10 = input("Answer 'a' or 'b': ")
            elif dif == 2:
                print(row[2] + "\n" + row[3])
                input10 = input("Answer 'a','b' or 'c': ")
            elif dif == 3:
                print(row[2] + "\n" + row[3] + "\n" + row[4])
                input10 = input("Answer 'a','b','c' or 'd': ")
            if input10 == row[dif + 2]:
                print("Correct")
                score = score + 1
                w = w + 1
            elif input10 != row[dif + 2]:
                print("Incorrect")
                w = w + 1
        if w == 9:
            print("Game over")
            print("You got", r, "right out of 10")
            break
相关问题