[split] help a brother am so new - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: [split] help a brother am so new (/thread-24422.html) |
[split] help a brother am so new - MohsinCoding - Feb-13-2020 Hi there, I am new with Python and would like anyone to help me with the following code: fibLimit= eval(input('Enter how many Fibonacci numbers you would like to print = ')) fbNext= [None] * int(fibLimit) fbNext[0] = 1 fbNext[1] = 1 for i in range(len(fbNext)): if i < 2: print(fbNext[i]) else: nex = fbNext[i] + fbNext[i - 1] print(nex)Enter how many Fibonacci numbers you would like to print = 5 1 1
RE: [split] help a brother am so new - Larz60+ - Feb-13-2020 1. Don't do this: fibLimit= eval(input('Enter how many Fibonacci numbers you would like to print = ')) instead: fibLimit= input('Enter how many Fibonacci numbers you would like to print = ')
RE: [split] help a brother am so new - jefsummers - Feb-14-2020 That is, well, kinda screwed up. Sorry. Here: fibLimit= int(input('Enter how many Fibonacci numbers you would like to print = ')) fbNext = [0,1] for i in range(fibLimit): if i<2 : print(fbNext[i]) else: fbNext.append(fbNext[i-2] + fbNext[i - 1]) print(fbNext[i])fibLimit should be int() rather than eval() fbNext can be initialized as [0,1] Any time you see range(len(xxx)) re-evaluate. That is almost always wrong. You want the range of fibLimit, not anything to do with fbNext. I wrote it so you append to the fbNext list as that seemed to be your preference, fixed the syntax and code. RE: [split] help a brother am so new - MohsinCoding - Feb-14-2020 @jefsummers: Thank You very Much But I had to change the starting as Fibonacci starts with "1". Also I am new to python so I agree that it was kinda screwed up The final code is: fibLimit= int(input('Enter how many Fibonacci numbers you would like to print = ')) fbNext = [1,1] for i in range(fibLimit): if i < 2: print(fbNext[i]) else: fbNext.append(fbNext[i-2] + fbNext[i - 1]) print(fbNext[i]) |