Ahoj, za prve, funguje vam prosim tento kod, me to vypisuje pouze "Odd numbers" nic vic
a za druhe v ucebici popisuji,ze pokud je uzito int (5/2) tak se napr. cislo 2,5 zaokrouhli na 2 apod. Takze pokud vygenerujeme nahodne cislo napr. 5, tak by podle ucebnice melo byt int(5/2) == 5/2 -> 2 == 2,5, jenze me to v shellu vypisuje 2 == 2 , navic v shellu mi i 5/2 - prava strana rovnosti vzdy vyhodi cele cislo 2 ne 2.5.
viz kniha: if int(number / 2) == number / 2:
. . . checks to see if the number is even. Remember, the int() function returns
only the whole portion of a number. So let’s say the random number that gets
generated is 5. Dividing this number by 2 gets you 2.5. Then int(number) is 2
because the int() of a number drops everything after the decimal point. 2 does
not equal 2.5, so the code skips over the continue , prints that odd number, incre-
ments the counter, and keeps going.
import random
print("Odd numbers")
counter= 0
while counter < 10:
number = random.randint(1,999)
if int(number/2) == number/2:
continue
print(number)
counter += 1
print("Loop is done")