簡體   English   中英

如果條件為假,如何跳出循環?

[英]How to break out of loop if the condition is false?

在我的程序中,我想輸出所有小於 10000 的數字(例如,如果我將數字 154 乘以 2,它應該只返回小於 10000 的值)。 但是,在我的程序中,我仍然得到大於 10000 的值。有人可以幫助我嗎?

這是我的程序:

import statistics

def verdoppeln(zahl):
    liste = []
    if zahl > 10000:
            print("Zahl nicht gültig.")
    else:
        print(zahl)
        while zahl < 10000:
            print(zahl * 2)
            zahl_neu = zahl * 2
            liste.append(zahl_neu)
            zahl = zahl_neu
            if zahl > 10000:
                print("Zur Kontrolle: Summe = ", sum(liste), "Anzahl: ", len(liste))
                print("Mittelwert: ", statistics.mean(liste))
        

verdoppeln(154)

問題是while -check 僅在每個循環開始時完成。 因此,如果zahl zahl * 2之后zahl * 2大於10000 ,它仍將被打印並添加到liste 如果您改變while終止條件,以True並從與內部打破while循環break語句來時的價值zahl變得大於10000 ,你應該得到你想要的結果。

此外,使用zahl_neu是不需要的,你可以使用zahl只,這將做工精細。

嘗試這個:

import statistics

def verdoppeln(zahl):
    liste = []
    if zahl > 10000:
            print("Zahl nicht gültig.")
    else:
        print(zahl)
        while True: # Changed the termination condition to True, in other words, never stop
            zahl *= 2
            if (zahl >= 10000): # Added termination condition from inside the loop
                break
            print(zahl)
            liste.append(zahl)
        print("Zur Kontrolle: Summe = ", sum(liste), "Anzahl: ", len(liste))
        if (len(liste) >= 1): # statistics.mean need liste to have at least one element
            print("Mittelwert: ", statistics.mean(liste))
        
verdoppeln(154)

這將輸出:

154
308
616
1232
2464
4928
9856
Zur Kontrolle: Summe =  19404 Anzahl:  6
Mittelwert:  3234

一個不間斷的替代方案:

import statistics

def verdoppeln(zahl):
    liste = []
    if zahl > 10000:
            print("Zahl nicht gultig.")
    else:
        print(zahl)
        zahl *= 2
        while zahl < 10000:
            print(zahl)
            liste.append(zahl)
            zahl *= 2            
        print("Zur Kontrolle: Summe = ", sum(liste), "Anzahl: ", len(liste))
        print("Mittelwert: ", statistics.mean(liste))
        
verdoppeln(154)

我認為你需要改變你的限制:

import statistics

def verdoppeln(zahl):
    liste = []
    if zahl >= 5000:
            print("Zahl nicht gültig.")
    else:
        print(zahl)
        while zahl < 5000:
            zahl *= 2
            print(zahl)
            liste.append(zahl)
        print("Zur Kontrolle: Summe = ", sum(liste), "Anzahl: ", len(liste))
        print("Mittelwert: ", statistics.mean(liste))
        

verdoppeln(154)
def verdoppeln(zahl):
    liste = []
    if zahl > 10000:
        print("Zahl nicht gültig.")
    else:
        while zahl < 10000:
            print(zahl)
            zahl = zahl * 2
            if zahl > 10000:
                print("Zur Kontrolle: Summe = ", sum(liste), "Anzahl: ", len(liste))
                print("Mittelwert: ", statistics.mean(liste))
            else:
                liste.append(zahl)

暫無
暫無

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

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