python 3.x - Why is my code not breaking out of my while loop? -
i have code display powers of two:
print ("how many powers of 2 see?") number=int(input()) values=[2] count=1 while count <= number: length=len(values) index=length-1 number=values[index]*2 values.append(number) count=count+1 print (values)
my code updating value of count, won't break out of while loop.
you compare count <= number
in every while loop, increase number
faster count
inside loop. don't touch number
in loop, this:
print("how many powers of 2 see?") number = int(input()) values = [2] count = 1 while count <= number: length = len(values) index = length - 1 number_tmp = values[index]*2 values.append(number_tmp) count += 1 print(values)
but more pythonic way like:
print("how many powers of 2 see?") number = int(input()) values = [2**(n+1) n in range(number)] print(values)
Comments
Post a Comment