For an assignment I need to write a piece of code which goes through numbers from 1 to 100 and only prints even ones and stops once they are printed. Yes, I know how to do it with a for loop but that's easy... I want to know if it is possible to do it with a while loop. Here is my very clunky code (the result of me constantly changing things around):
import random
counter = 1
while (counter != 50):
number = random.randrange(1, 100)
if (int(number) % 2 == 0):
print (number)
counter = counter + 1
The problem with that is that it repeats numbers. It needs to only print each number once. And yes I have tried using "random.sample" but that has a problem with ints.
Aucun commentaire:
Enregistrer un commentaire