0% found this document useful (0 votes)
5 views1 page

Python Fibonacci Sequence Generator

This document contains a Python program that generates the Fibonacci sequence up to a specified number of terms. It includes a function to compute the sequence and a main function that handles user input and output. The program also includes error handling for invalid inputs.

Uploaded by

pukibabal1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Python Fibonacci Sequence Generator

This document contains a Python program that generates the Fibonacci sequence up to a specified number of terms. It includes a function to compute the sequence and a main function that handles user input and output. The program also includes error handling for invalid inputs.

Uploaded by

pukibabal1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Program

December 24, 2025

1 | # Sample Python Program - Fibonacci Sequence


2 | def fibonacci(n):
3 | """Generate Fibonacci sequence up to n terms"""
4 | if n <= 0:
5 | return []
6 | elif n == 1:
7 | return [0]
8 | elif n == 2:
9 | return [0, 1]
10 |
11 | sequence = [0, 1]
12 | for i in range(2, n):
13 | next_num = sequence[i-1] + sequence[i-2]
14 | [Link](next_num)
15 |
16 | return sequence
17 |
18 | def main():
19 | # Get user input
20 | try:
21 | num_terms = int(input("Enter number of terms: "))
22 |
23 | if num_terms < 0:
24 | print("Please enter a positive number")
25 | else:
26 | result = fibonacci(num_terms)
27 | print(f"Fibonacci sequence: {result}")
28 |
29 | except ValueError:
30 | print("Invalid input! Please enter a number.")
31 |
32 | if __name__ == "__main__":
33 | main()

Page 1 of 1

You might also like