如何在python中从第二个代码块到第一个缩进块分配布尔值?

时间:2019-04-16 07:12:19

标签: python loops indentation assign

在第5个块中分配out_of_marks_limit = True时,我希望if语句的第一个块为“ True”,并且我不希望代码循环或再次询问用户。

在其他编程语言中,缩进用于使程序看起来不错。但是因为python仅将第一个块的条件检查为相同的缩进,所以我无法将2个代码块的布尔值分配给第一个块。 This is what I'm trying to say.

我是中级程序员,该程序仅用于练习。

a = int(input("Enter no. of subjects: "))
count = 1
d = 0
out_of_marks_limit = False          #I want the out_of_limit to be True
if a <= 10:
    if not out_of_marks_limit:
        if count <= a:
            for c in range(a):
                b = float(input("Enter mark of subject " + str(count) + ": "))
                if b <= 100:        #If this condition went false then it will skip to else statement
                    d += b
                    count += 1
                    if count > a:
                        cal = round(d/a, 2)
                        print("Your percentage is " + str(cal) + "%")
                else:
                    out_of_marks_limit = True #So this boolean value should passed to the first line of code
                    print("Marks enter for individual subject is above 100")
else:
    print("Subject limit exceeded")

如果out_of_marks_limit为True,并且不想再循环播放,则希望输出输出(“单个主题的分数输入高于100”)

1 个答案:

答案 0 :(得分:0)

我认为您可以使用while循环检查out_of_marks_limit条件:

a = int(input("Enter no. of subjects: "))
count = 1
d = 0
out_of_marks_limit = False          #I want the out_of_limit to be True

while not out_of_marks_limit:
    if a <= 10:
        if not out_of_marks_limit:
            if count <= a:
                for c in range(a):
                    b = float(input("Enter mark of subject " + str(count) + ": "))
                    if b <= 100:        #If this condition went false then it will skip to else statement
                        d += b
                        count += 1
                    if count > a:
                        cal = round(d/a, 2)
                        print("Your percentage is " + str(cal) + "%")
                    else:
                        out_of_marks_limit = True #So this boolean value should passed to the first line of code
                        print("Marks enter for individual subject is above 100")
else:
    print("Subject limit exceeded")