This is a Python program that generates the Fibonacci sequence up to a specified number of terms. It’s perfect for learning about the Fibonacci sequence and improving Python programming skills.
- Generates the Fibonacci sequence up to any given number of terms.
- Friendly terminal interface with formatted output.
- Handles invalid inputs with appropriate error messages.
- Beginner-friendly Python code.
-
Input: The user is asked to input a positive integer for the number of terms in the Fibonacci sequence.
- If the input is valid, the program calculates and displays the Fibonacci sequence up to the specified number.
- If the input is invalid (e.g., non-integer or negative), the program prompts the user to enter a valid number.
-
Processing: The Fibonacci sequence is generated using a simple iterative approach where each term is the sum of the two preceding ones:
F(n) = F(n-1) + F(n-2)
-
Output: The program prints the Fibonacci sequence up to the desired number of terms, with clear formatting for easy reading.
Ensure you have Python installed on your machine. If not, download it from the official website: Python Download.
Clone or download this repository:
git clone https://github.com/mdriyadkhan585/fibonacci-sequence-generator-python.git
cd fibonacci-sequence-generator-python
You can run the Python script by navigating to the directory and executing the following command:
python fibonacci.py
You will see the following prompt in the terminal:
====================================
Fibonacci Sequence Generator
====================================
Enter the number of terms you want to generate:
Here’s an example of the program’s output when the user enters 6
as the number of terms:
====================================
Fibonacci Sequence Generator
====================================
Enter the number of terms you want to generate: 6
Fibonacci Sequence up to 6 terms:
0, 1, 1, 2, 3, 5
====================================
Program Ended
====================================
- If the user enters a non-positive number or an invalid input:
Please enter a valid positive integer.
The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding numbers. It typically starts with 0 and 1.
This program generates a finite sequence based on the number of terms you specify. To generate an infinite sequence, you’d need to modify the code or run it in an infinite loop (though this is not recommended for normal use).
Absolutely! This program is beginner-friendly, and the code is well-structured to help new Python learners understand loops, conditionals, and basic error handling.
This project is licensed under the MIT License. See the LICENSE file for more details.
Happy coding! 💻✨