Last active
February 14, 2022 06:06
-
-
Save wakoliVotes/3111f753f452bcfc43f66b18a033b411 to your computer and use it in GitHub Desktop.
Python Function to Display Fibonacci Sequence given specific number of item counts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to create Fibonacci Sequence given that the user defines number of values | |
# First, use the "def" keyword to define the function | |
def fibonacciSequence(): | |
# Next, asking users to state the proportion or number of sequence terms to generate | |
numberOfTerms = int(input("Hi, enter number of terms to for the Sequence: ")) | |
# Defining the first two terms that are mandatory | |
# Normally, 0 and 1 are the first and second terms of the series, hence: | |
firstNum = 0 | |
secondNum = 1 | |
count = 0 | |
# Ensuring the user enters valid inputs, i.e., Fibonacci sequence starts from 0 | |
if (numberOfTerms <= 0): | |
print("Invalid entry, please enter value more than 0") | |
elif(numberOfTerms == 1): | |
print("Generating Fibonacci Sequence upto ", numberOfTerms, ": ") | |
print(firstNum) | |
# For all other cases, i.e., when NumberOfTerms > 1 | |
else: | |
print("Generating Fibonacci sequence upto ", numberOfTerms, ": ") | |
while count < numberOfTerms: | |
print(firstNum) | |
nthNumber = firstNum + secondNum | |
# Swapping the values for firstNum and secondNum | |
firstNum = secondNum | |
secondNum = nthNumber | |
count += 1 | |
# Call the function | |
fibonacciSequence() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment