1
def func(k): 
    print k
    i=0
    numbers = []
    while i<k:
        print "At the top i is %d" %i
        numbers.append(i*i*i)
        i=i+1
        print "The numbers are: " , numbers




y = raw_input("Give me a number")
m=func(y)

I am learning using functions and loops in python. As soon as i run this code it starts printing infinite numbers. I'm unable to find the issue with it.

1
  • 1
    Try y = int(raw_input("Give me a number")) and tell us how that goes :) Commented Sep 16, 2015 at 1:59

2 Answers 2

3

The function raw_input() returns a string, and then you are passing that string onto the function func().

In func() , in the condition for the while loop, you are checking an int against a string.

In Python 2.x , any int is always smaller than any string , hence the loop goes on forever. Example -

>>> 12323123123123121 < '1'
True

You should convert the input to an integer before giving it to the function (or directly when taking the input) . Example -

y = int(raw_input("Give me a number"))

Also, since you are expecting a return value from your function, you should return a value. Currently, you are not returning anything from the function. Example -

while i<k:
    ...
    i=i+1
    print "The numbers are: " , numbers
return numbers

In Python 3.x , this type of comparison is not allowed, if you try to compare string and int with that operator, you would get an error like - TypeError: unorderable types: int() < str() .

Sign up to request clarification or add additional context in comments.

Comments

2

There are a couple of big problems with your code:

  1. your input is a string, not a number
  2. there's no return statement

To solve 1, you must first realize that raw_input gets... raw input; specifically in the form of a str. You must convert it to an int, float, whatever you want. I'll presume you want an integer input for sake of simplicity.

y = int(raw_input("Give me a number"))

Without a return, you can't return a value. In this case, you can't return the m that you want. I'll assume that you want to return numbers once your loop condition fails.

def func(k): 
  print k
  i=0
  numbers = []
  while i<k:
    print "At the top i is %d" %i
    numbers.append(i*i*i)
    i=i+1
    print "The numbers are: " , numbers
  return numbers

I wrote a little script to test -- here it is:

def func(k):
  print k
  i=0
  numbers = []
  while i<k:
    print "At the top i is %d" %i
    numbers.append(i*i*i)
    i=i+1
    print "The numbers are: " , numbers
  return numbers

hello = func(3)
print hello

The output I get is:

3
At the top i is 0
The numbers are:  [0]
At the top i is 1
The numbers are:  [0, 1]
At the top i is 2
The numbers are:  [0, 1, 8]
[0, 1, 8]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.