簡體   English   中英

如何獲得連續相同的輸入來中斷循環?

[英]How do I get consecutive same input to break loop?

我正在嘗試制作一個學生學分計算器。 它必須顯示文本:當用戶連續兩次輸入 0 時, You did have too many study breaks 代碼也應該到此結束。 我什至不知道要開始解決這個問題,但我已經准備好代碼的 rest。

def main():

    months=int(input("Enter the number of months: "))
    total=0

    for i in range(months):
        points=float(input("Enter the number of credits in month {}: ".format(i+1)))
        total += points

    average=total/months
    if average >= 5:
        print(f"You are a full time student and your monthly credit point average is {average:.1f}")
    elif average < 5:
        print(f"Your monthly credit point average {average:.1f} does not classify you as a full time student.")



if __name__ == "__main__":
    main() 

因此,為了澄清,如果用戶連續兩次輸入 0 點,則循環應該中斷並顯示文本。 先感謝您。

只需跟蹤先前的輸入:

prev = -1
for i in range(months):
    points=float(input("Enter the number of credits in month {}: ".format(i+1)))
    if not (prev or points):
        break
    total += points
    prev = points

您可以使用變量來跟蹤上次輸入的前一個值。 然后檢查這個“prev”變量並在每次迭代時與你的“current”變量進行比較。 在獲得新輸入之前設置此“prev”值很重要。

如果它們都相等,則您知道用戶連續輸入了相同的輸入。

例子:

def main():

    months=int(input("Enter the number of months: "))
    total=0
    prev=0
    data = 0

    for i in range(months):
        prev = data
        data = input("Enter the number of credits in month {}: ".format(i+1))
        if ((prev==data) && data == 0): 
            print("You have too many study breaks")
            break

        points=float(data)
        total += points

    average=total/months
    if average >= 5:
        print(f"You are a full time student and your monthly credit point average is {average:.1f}")
    elif average < 5:
        print(f"Your monthly credit point average {average:.1f} does not classify you as a full time student.")



if __name__ == "__main__":
    main() 

如果需要 function 已經在其中,這是帶有結尾的代碼:

def main():
    pointsEnd = 0
    months=int(input("Enter the number of months: "))
    total=0

    for i in range(months):
        points=float(input("Enter the number of credits in month {}: ".format(i+1)))
        if points == 0: #here is the new code
            pointsEnd += 1
            if pointsEnd == 2:
                end = print('You did have too many study breaks')
                return end
                ()
        total += points

    average=total/months
    if average >= 5:
        print(f"You are a full time student and your monthly credit point average is {average:.1f}")
    elif average < 5:
        print(f"Your monthly credit point average {average:.1f} does not classify you as a full time student.")



if __name__ == "__main__":
    main() 

您可以創建一個新變量來計算重復次數,並添加一個“if 語句”,其中 if 語句以“break”結尾

def main():

    //counter
    duplicates = 0

    months=int(input("Enter the number of months: "))
    total=0

    for i in range(months):
        points=float(input("Enter the number of credits in month {}: ".format(i+1)))

        //Add checks if it is a duplicate, if there are 2 or more duplicate, end 
        //for sentence

        if points == 0:
            duplicates += 1
        else:
            duplicates = 0

       if duplicates >= 2:
            break

        total += points

    average=total/months
    if average >= 5:
        print(f"You are a full time student and your monthly credit point average is {average:.1f}")
    elif average < 5:
        print(f"Your monthly credit point average {average:.1f} does not classify you as a full time student.")



if __name__ == "__main__":
    main() 

暫無
暫無

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

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