lundi 27 septembre 2021

Finding the biggest number for a sequence given by the while loop

I'm currently learning Python, more specifically, how the loop while works. As far as I know, this loop can be used to create sequences of numbers with and specific condition. For example,

n=1

while n < 10:
    n = n*2
    print(n)

Which gives the powers of two up to 16:

2
4
8
16

But I was wondering if there's a way for the loop to print only the biggest number of the sequence, in this case, it would be 16. Here's my attempt,

n=1
bigNumber = 0
while n <= 10:
    bigNumber = n
    n = n*2
    if n < bigNumber:
        print(bigNumber)

However it doesn't work. Could someone point out were the mistake is?

Aucun commentaire:

Enregistrer un commentaire