gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
Write python code to show the fibonacci sequence.
- User
Write python code to show the fibonacci sequence.
- Assistant
Certainly! The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. Here's a simple Python program to generate the Fibonacci sequence up to a specified number of terms:
pythondef fibonacci_sequence(n): if n <= 0: print("The number of terms must be a positive integer.") return [] elif n == 1: return [0] elif n == 2: return [0, 1] sequence = [0, 1] while len(sequence) < n: next_value = sequence[-1] + sequence[-2] sequence.append(next_value) return sequence # Change the value of num_terms to generate more or fewer terms num_terms = 10 fib_sequence = fibonacci_sequence(num_terms) print(f"Fibonacci sequence with {num_terms} terms: {fib_sequence}")This code defines a function
fibonacci_sequence(n)that generates the Fibonacci sequence up to terms. You can adjust the variable to get more or fewer terms of the sequence.