0

I placed a while-loop inside of a function 'count()'.

Ideally, I want the user to pick a number between one and 10. If said number "n" < the base number (i), then print and add 1.

The problem is that when n > i, the process continues as if being ignored.

i = 0
numbers = []

print "Pick a number between 1 and 10:"
n = raw_input("> ")

def count(x,y):
    while x < y:
        print "First number is %d" % x
        numbers.append(x)

        x += 1
        print "Total numbers now: ", numbers
        print "Last number = %d" % x

count(i,n)

I believe this has something to do with the While-Loop being placed inside of a function, or perhaps I'm not seeing it.

My returned product looks something like this:

At the top, i is 9
Numbers now:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
At the bottom i is 10

I am still learning Python (my first language) and trying to follow a tutorial. I am aware that it may be better to do For-Loops instead, however this is the current topic I'm still trying to grasp.

Zachary M.
  • 17
  • 1
  • 9
  • 1
    In your code here, `n` is a string, not a number. – khelwood Apr 05 '17 at 19:26
  • Briefly: you need to cast `n` to an integer, because `raw_input` always produces strings, and Python 2 will happily compare objects of different types with no error messages because it is bad. – TigerhawkT3 Apr 05 '17 at 19:27
  • @TigerhawkT3 Wait but how is the condition met when `i != 0`? Isn't all strings and all non-zero integers considered to be `True`? – spicypumpkin Apr 05 '17 at 19:32
  • 1
    @Posh_Pumpkin - Non-empty strings and non-zero numbers (and non-empty sequences, etc.) are truthy. However, when you compare objects of different types in Python 2 (e.g. `'a' > 4`), it will compare their _types_, alphabetically, so strings are always greater than integers. – TigerhawkT3 Apr 05 '17 at 19:33
  • @ZacharyM: Please unaccept my answer so that I can delete it and make Tigerhawk happy. – Eric Duminil Apr 05 '17 at 19:41

0 Answers0