Here’s a Python program to print the Fibonacci series up to a specified number of terms:
# Function to print Fibonacci series
def fibonacci_series(n):
# First two terms
a, b = 0, 1
print("Fibonacci Series:")
for i in range(n):
print(a, end=" ")
# Update the values of a and b
a, b = b, a + b
# Input number of terms from the user
n = int(input("Enter the number of terms: "))
# Call the function
fibonacci_series(n)This program prompts the user to input the number of terms and then prints the Fibonacci series accordingly.
The provided Fibonacci series program works as expected, but there are a few potential issues and areas for improvement:
- Problem: If the user inputs a non-integer value or a negative number, the program will either crash or produce incorrect results.
- Improvement: Add input validation to ensure the input is a positive integer.
- Problem: If the user enters 0 or 1 as the number of terms, the program will behave strangely by printing nothing or just
0. - Improvement: Add specific conditions to handle the cases where the input is
0or1.
- Problem: Printing the series inside the loop limits flexibility (e.g., reusing the function to return the series instead of just printing it).
- Improvement: Modify the function to return the Fibonacci series as a list, allowing it to be reused elsewhere or printed later.
- Improvement Option: While the iterative approach is efficient, a recursive approach can provide a more elegant solution, though at the cost of performance for large inputs.
def fibonacci_series(n):
# Check for invalid input
if n < 0:
return "Invalid input! Please enter a positive integer."
elif n == 0:
return []
elif n == 1:
return [0]
# Generate the Fibonacci series
fib_series = [0, 1]
for i in range(2, n):
fib_series.append(fib_series[-1] + fib_series[-2])
return fib_series
# Input number of terms with validation
while True:
try:
n = int(input("Enter the number of terms: "))
if n < 0:
print("Please enter a positive integer.")
else:
break
except ValueError:
print("Invalid input! Please enter an integer.")
# Call the function and print the series
result = fibonacci_series(n)
if isinstance(result, list):
print("Fibonacci Series:", " ".join(map(str, result)))
else:
print(result)- Input Validation: The program now checks whether the input is a valid integer and ensures that it is non-negative.
- Handling Edge Cases: It handles edge cases for
0and1terms. - Return Value: The function now returns a list of Fibonacci numbers, allowing for flexibility (e.g., printing, further manipulation, etc.).
- User-Friendly Messages: Added appropriate messages for invalid input.