Python Forum
[split] help a brother am so new
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] help a brother am so new
#1
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

Error:
Traceback (most recent call last): File "D:My Python Code\CHP2.py", line 40, in <module> nex = fbNext[i] + fbNext[i - 1] TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Reply
#2
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 = ')
Reply
#3
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.
Reply
#4
@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])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help a brother am so new manimani 5 2,269 Feb-14-2020, 11:22 PM
Last Post: michael1789

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020