I am new to python and to programming all together. I am wondering if this is a terribly inefficient way of generating Fibonacci numbers for a beginner?
a = 1
b = 1
total = 0
counter = input("Please enter the term you wish to end at: ")
print "1"
print""
print "1"
number = 2
while counter > number:
total = a+b
print ""
print total
a = b
b = total
number = number + 1
If so, can someone point out a few things such as:
What to research/Google to make my code more efficient.
Suggest programming practices that I need to work on (I know this isn’t a huge sample of my work).
With python you do not want to worry about efficiency as much as you would in C for example, although you still want to achieve the shortest Big Oh running time. The way you have written this is around as efficient as you can get so you don’t need to worry about that too much. However, it is not very pythonic with the use of
whileto add to a counter.This can be written more simply as:
You could also use a generator for this, which might be a programming practice you could work on…