簡體   English   中英

如何將第二個代碼塊中的布爾值分配給python中的第一個縮進塊?

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

當我在第5個塊中分配out_of_marks_limit = True時,我希望if語句的第一個塊為“True”,並且我不希望代碼循環或再詢問用戶。

在其他編程語言中,縮進用於使程序看起來很好。 但是因為python只檢查第一個塊到相同縮進的條件,所以我不能將2個代碼塊的布爾值分配給第一個塊。 這就是我想說的。

我是一名中級程序員,這個程序僅用於練習目的。

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並且不想再循環

我認為您可以使用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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM