From 97013b1639908123fb7f28565c8e3fae2cc45873 Mon Sep 17 00:00:00 2001 From: ZoyaQadeer Date: Sat, 19 Oct 2024 15:29:33 +0530 Subject: [PATCH] Update fibonacci.py --- fibonacci.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/fibonacci.py b/fibonacci.py index fee3de2..68dde1d 100644 --- a/fibonacci.py +++ b/fibonacci.py @@ -1,4 +1,5 @@ # fibonacci.py + def fibonacci(n): a, b = 0, 1 sequence = [] @@ -8,5 +9,11 @@ def fibonacci(n): return sequence if __name__ == "__main__": - n = int(input("Enter the number of Fibonacci terms: ")) - print(f"Fibonacci sequence: {fibonacci(n)}") + try: + n = int(input("Enter the number of Fibonacci terms: ")) + if n < 0: + print("Please enter a non-negative integer.") + else: + print(f"Fibonacci sequence: {fibonacci(n)}") + except ValueError: + print("Invalid input. Please enter a valid integer.")