0

I wrote a small module --

def fib(n):     #write Fibonacci series up to n
    a, b = 0, 1
    while b < n:
            print b,
            a, b = b, b+a

def fib2(n):    #return Fibonacci series up to n
    result = []
    a, b = 0, 1
    while b < n:
            result.append(b)
            a, b = b, a+b
    return result

Then I imported it into another script fibonacci.py

from fibo import fib

num = raw_input('Enter the value of N :: ')
print fib(num)

When I run fibonacci.py,the prompt asks me to enter the value of N. When I do so and press return, it starts printing numbers ans stuck in an infinite loop. The printing of numbers continues and continues...

What I might have been doing wrong?

Tim
  • 41,901
  • 18
  • 127
  • 145
MAB
  • 137
  • 1
  • 2
  • 11
  • I removed the code for `fib2` function because it seemed unrelated – Tim Jun 09 '14 at 11:47
  • 3
    @TimCastelijns This is not appropriate edit. You should not change the original meaning of the post, regardless of what seems related or not to you. – BartoszKP Jun 09 '14 at 11:48
  • 1
    @BartoszKP `fib2` was never called, so it is not related to the issue here. – Rob Watts Jun 09 '14 at 11:49
  • 2
    @BartoszKP I disagree that it changes the original meaning because it is was unused code and only caused confusion – Tim Jun 09 '14 at 11:49
  • @Mridu just use `int(raw_input('Enter ...'))` and it should work. – Rob Watts Jun 09 '14 at 11:50
  • @TimCastelijns People are not compilers. The fact that it wasn't called is irrelevant. It could have been important to better understand what OP is up to. The fact that it's not important to you doesn't mean it won't be important to others. Even if in this particular question it no harm was done this is just not the way we edit questions on SO. – BartoszKP Jun 09 '14 at 11:51
  • @TimCastelijns Engaging in edit/rollback wars is also not welcome here, I flagged for moderator attention. – BartoszKP Jun 09 '14 at 11:52
  • @BartoszKP I see your point and i rolled back to your rollback because i could not disagree with your previous point – Tim Jun 09 '14 at 11:53
  • @TimCastelijns All right, thanks. Seems I was too hasty with flagging... – BartoszKP Jun 09 '14 at 11:54
  • @Rob Watts Thanks. I'm a new programmer and after posting this question I was trying to debug and realized that my input was actually a string and I needed it to convert to a number to make the while comparison check work. Thanks a lot for your answer too. _On a separate note, I see you have marked my question duplicate. I'm a new user here and do not know how to play with the rules. Had I know that problem was with raw_input, I'd not have this problem at the first place. So should I delete my question?_ – MAB Jun 09 '14 at 12:38
  • @MriduBhattacharya Don't worry about it. Your question isn't bad, but the underlying problem is fairly common. – Rob Watts Jun 09 '14 at 13:04

0 Answers0