-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
Ma0
  • 15,057
  • 4
  • 35
  • 65
  • what kind of values does `raw_input()` return? (to be more specific, *which* values does it normally return?) (are they always numbers?) – Timothy Groote Nov 17 '17 at 08:07
  • 1
    Change the assignment of `x` to `x = int(raw_input())`. That way it will be an integer rather than a string. – Tom Karzes Nov 17 '17 at 08:08

2 Answers2

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.

Mr. Xcoder
  • 4,719
  • 5
  • 26
  • 44
0

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
Asa Stallard
  • 322
  • 1
  • 14