在我的程序中,我希望输出所有小于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
-检查只在每个循环的开始处进行。因此,如果zahl
大于10000
在zahl * 2
之后,它仍将被打印并添加到liste
。如果将while
终止条件更改为True
,并在zahl
的值大于10000
时使用break
-语句从内部中断while循环,则应该会得到所需的结果另外,不需要使用
zahl_neu
,您可以只使用zahl
,这样就可以了试试这个:
这将输出:
我认为你需要改变你的限制:
相关问题 更多 >
编程相关推荐