I am trying to print "count" , when count is smaller than my input value , but when I give the input value for X, it loos forever. Can anybody tell me why ?
count = 0
x= raw_input()
while count <x :
print (count )
count +=1
I am trying to print "count" , when count is smaller than my input value , but when I give the input value for X, it loos forever. Can anybody tell me why ?
count = 0
x= raw_input()
while count <x :
print (count )
count +=1
By looking at the behaviour of the comparison operators (<
, >
, ==
, !=
), you can check that they treat integers as being smaller than non-empty strings. raw_input()
returns a string (rather than an integer as you expected), hence your while
loops indefinitely. Just switch to input()
:
count = 0
x = input()
while count < x:
print(count)
count += 1
Alternatively, you can use int(raw_input())
, but I always use (and prefer) the former. All this is assuming you use Python 2.
Cast the input as an int so that the loop can increment it:
count = 0
x = int(raw_input())
while count <x :
print (count )
count +=1