如何将第二个代码块中的布尔值分配给python中的第一个缩进块?

问题描述 投票:0回答:1

当我在第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")

我希望输出打印(“单个主题的标记输入超过100”),如果out_of_marks_limit为True并且不想再循环

python loops indentation assign
1个回答
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")
© www.soinside.com 2019 - 2024. All rights reserved.