for loop - Use value of a variable as counter in python 3 -
i want take input user , and store in variable, lets k
. use k
counter for loop
.
while i<k:
doesn't work! code:
k= input('number of points:') p=[] i=0 while i<k: x=float(input('enter value=')) p.append(x) i=i+1
output:
number of points:3 traceback (most recent call last): file "/home/ramupradip/tes.py", line 4, in <module> while i<k: typeerror: unorderable types: int() < str()
i tried using range
for in range(1,k)
which gave me error: traceback (most recent call last): file "/home/ramupradip/reflect.py", line 6, in in range(1,k): typeerror: 'str' object cannot interpreted integer
read input this:
k = int(input('enter counter: '))
... guess forgot convert number int
, it's impossible tell if don't show relevant code. other that, looping constructs shown in question should work fine, first 1 preferred in python, careful indexes:
for in range(1, k+1): #
equivalently:
i = 1 while <= k: # += 1
Comments
Post a Comment