From ccab626f28418fbd4ccbf66687d0237264d551d4 Mon Sep 17 00:00:00 2001 From: Abdeali kanchwala <163712510+Abdeali916@users.noreply.github.com> Date: Sat, 19 Oct 2024 15:25:55 +0530 Subject: [PATCH] Update factorial.py --- factorial.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/factorial.py b/factorial.py index 1d04ecd..d0215d8 100644 --- a/factorial.py +++ b/factorial.py @@ -5,5 +5,13 @@ def factorial(n): return n * factorial(n - 1) if __name__ == "__main__": - num = int(input("Enter a number: ")) - print(f"The factorial of {num} is {factorial(num)}") + while True: + try: + num = int(input("Enter a positive integer: ")) + if num < 0: + print("Please enter a positive integer.") + else: + print(f"The factorial of {num} is {factorial(num)}") + break # Exit the loop if valid input + except ValueError: + print("Invalid input. Please enter a positive integer.")