Though this program tells the truth (unless you change the source code), it does tell Fibonacci numbers, depending on the user's input. Keeping in mind of all (or most) of the feedback given from previous questions, I wrote this program to see how well I have progressed.
def get_fibonacci_sequence(max):
'''
Returns the Fibonacci sequence from the
first to the nth Fibonacci number.
'''
fib1 = 1
fib2 = 1
result = [fib1, fib2]
for i in range(2, max):
result.append(fib1 + fib2)
fib1 = fib2
fib2 = result[i]
return result
def get_nth_fibonacci(n):
'''Returns the nth Fibonacci number.'''
return get_fibonacci_sequence(n)[n - 1]
def main():
print("Would you like to have the sequence or the number?")
print("Enter 's' for sequence, and anything else for the number.")
seqOrNum = input("> ")
if seqOrNum == 's':
print("Up to which number would you like the sequence up to?")
max = int(input("> "))
print("The sequence up to the {}th Fibonacci number:".format(max))
print(get_fibonacci_sequence(max))
else:
print("Which Fibonacci number would you like to get?")
max = int(input("> "))
print("The {}th Fibonacci number is:".format(max),
get_nth_fibonacci(max))
if __name__ == "__main__":
main()
Example Output:
Would you like to have the sequence or the number?
Enter 's' for sequence, and anything else for the number.
> s
Up to which number would you like the sequence up to?
> 10
The sequence up to the 10th Fibonacci number:
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
Concerns:
- Are my docstrings well-used?
- Does it follow conventions?