Python Forum
While loop within a Function (2.7) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: While loop within a Function (2.7) (/thread-3829.html)



While loop within a Function (2.7) - tuffgong - Jun-28-2017

Good Morning. I have some beginner/intermediate coding experience and decided to learn a new language. I am working through a book that teaches Python 2.7. This question seems pretty simple, I made a While loop that worked. Then I was asked to put it in a function and I am getting a syntax error on Line 9 - While i < stop:. Is the indention wrong? Thanks in advance for any help on this.

stop = raw_input("How high should we count? ")
increment = raw_input("What should we count by? ")


def list_numbers(stop, increment):
i = 0
numbers = []

While i < stop:
print "At the top i is %d" % i
numbers.append(i)

i = i + 1
print "Numbers now: ", numbers
print "At the bottom i is %d" % i

print "The numbers: "

for num in numbers:
print num



list_numbers(stop, increment)



RE: While loop within a Function (2.7) - Larz60+ - Jun-28-2017

Since you are in the learning stage, It would be a good idea to buy another book that uses python 3
As far as your code is concerned, your statement
stop = raw_input("How high should we count? ")
inputs a string, not an integer, so when you use the statement
While i < stop:
you are trying to compare an int to a string.

there are other issues, not checking validity of input value, etc. but another issue.
change your input statement to read:
stop = int(raw_input("How high should we count? "))
Again, you will need assert that the input is capable of integer conversion.
Sooner or later someone will enter non valid input and blow the program up


RE: While loop within a Function (2.7) - tuffgong - Jun-28-2017

I knew it was something simple. Thanks so much for the quick reply and the advice. Any recommendations on Python 3 books would be great.


RE: While loop within a Function (2.7) - sparkz_alot - Jun-28-2017

Please refer to our list of Free Python Resources