简体   繁体   中英

How do I assign a boolean from second block of code to the first block of indentation in python?

When I assign the out_of_marks_limit = True in 5th block, I want the first block of if statement to be "True" and I don't want the code to loop or ask the user anymore.

In other programming language indentation is used to make the program look good. But because python only checks the condition of first block to the same indent, I can't assign boolean of 2 block of code to the first block. This is what I'm trying to say.

I'm an intermediate level programmer and this program is just for practice purpose.

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")

I expect the output to print("Marks enter for individual subject is above 100"), if out_of_marks_limit is True and don't want to loop anymore

I think you can use a while loop to check your out_of_marks_limit condition:

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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