From 62fcb285797ed3d77c9f7a69d17402261506a659 Mon Sep 17 00:00:00 2001 From: tahirpukhta <152134239+tahirpukhta@users.noreply.github.com> Date: Wed, 13 Dec 2023 21:17:49 +0530 Subject: [PATCH] Update 9_for_exercise.py only two valid inputs. either "yes" or "no" --- Basics/Exercise/9_for/9_for_exercise.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Basics/Exercise/9_for/9_for_exercise.py b/Basics/Exercise/9_for/9_for_exercise.py index 1860abd1..2c35a923 100644 --- a/Basics/Exercise/9_for/9_for_exercise.py +++ b/Basics/Exercise/9_for/9_for_exercise.py @@ -62,6 +62,24 @@ print("Hurray! You are a rock star! You just finished 5 km race!") else: print("You didn't finish 5 km race but hey congrats anyways! You still ran {i+1} miles") +#I wanted to only take two valid inputs. Either "yes" or "no"(case-insensitive). That's why I tried the code below: +for i in range(1,6): + while True: + yorn = input(f"You ran {i} miles. Are you tired? ") + if yorn.lower() == "yes": + if i<5: + print(f"you didn't finish the race but still ran {i} miles") + else: + print(f"Congratulations! You have finished the race by running {i} miles.") + break + elif yorn.lower() == "no": + break + else: + print("Invalid input! Please enter either 'yes' or 'no'.") + if yorn.lower() == "yes": + break +else: + print(f"congratulations!!! You have finished the race by running {i} miles.") # 5. Write a program that prints following shape # ``` @@ -77,4 +95,4 @@ s = '' for j in range(i): s += '*' - print(s) \ No newline at end of file + print(s)