繁体   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