zahl = 0
for i in range(4):
zahl += 3*i
print (zahl)
Ich bin absoluter Anfänger also nicht wundern. Warum werden als Zahlen nicht 0, 3, 6, 9 ausgegeben sondern 0, 3, 9, 18.
Es wird ja wie folgt gerechnet:
0 + 3 * 0 = 0
0 + 3* 1 = 3
0 + 3 * 2 = 6
0 + 3 * 3 = 9
for-Schleife
- __blackjack__
- User
- Beiträge: 14332
- Registriert: Samstag 2. Juni 2018, 10:21
- Wohnort: 127.0.0.1
- Kontaktdaten:
Vor allem wäre es ja auch sinnlos zu jedem Ergebnis in der Schleife 0 dazu zu addieren, weil das ja gar keinen Effekt hätte.
„Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.“ — Brian W. Kernighan
- pillmuncher
- User
- Beiträge: 1532
- Registriert: Samstag 21. März 2009, 22:59
- Wohnort: Pfaffenwinkel
Code: Alles auswählen
a += b entspricht a = a + b.
zahl += 3 * i entspricht folglich:
zahl = zahl + 3 * i
0 == 0 + 3 * 0
\
\
\
\
3 == 0 + 3 * 1
\
\
\
\
9 == 3 + 3 * 2
\
\
\
\
\
18 == 9 + 3 * 3
In specifications, Murphy's Law supersedes Ohm's.
