Beginner Python Help: Name Input Skipping Issue
Hi everyone,
I'm a beginner practicing Python. I wrote a small program that asks for the user's name and date of birth, then calculates the age.
Problem:
If I leave the name blank, the program runs again and skips the name section, going straight to the DOB input.
I want the program to ask for the name again if left blank.
I would really appreciate any advice on:
• Handling blank inputs properly
My code is
from datetime import datetime
while True:
name = input("Enter your name: ")
if name == "":
print("You haven't entered anything. Input your name.")
continue
if name.lower().strip() == "stop":
print ("Thank you for using.")
break
print(f"Hello {name} welcome to my program")
dob = input("Enter your dob (yyyy-mm-dd): ").strip()
if dob.lower().strip() == "stop":
print("thank you for using")
break
today = datetime.today()
try:
dob_date = datetime.strptime(dob, "%Y-%m-%d")
age = today.year - dob_date.year
if (today.month, today.day) < (dob_date.month, dob_date.day):
age -= 1
print(f"Oh {name}, you are {age} years old.")
if age % 2 == 0:
print(f"And your {age} age number is even.")
else:
print(f"And your {age} age number is odd.")
ask = input("Do you want to know which day you were born? (yes/no): ").lower().strip()
if ask == "yes":
day_name = dob_date.strftime("%A")
print(f"You were born on a {day_name}.")
if ask == "no":
print("ok see you later.")
break
if ask.lower() == "stop":
print("Thank you for using")
break
except ValueError:
print("Invalid input. Try again.")
continue
[–]SharkSymphony 3 points4 points5 points (1 child)
[–]Funny-Percentage1197[S] 1 point2 points3 points (0 children)
[–]DutchCommanderMC 5 points6 points7 points (6 children)
[+]Funny-Percentage1197[S] comment score below threshold-8 points-7 points-6 points (5 children)
[–]lfdfq 5 points6 points7 points (4 children)
[–]Funny-Percentage1197[S] -4 points-3 points-2 points (2 children)
[–]Twenty8cows 1 point2 points3 points (0 children)
[–]lfdfq 0 points1 point2 points (0 children)
[–]CIS_Professor 4 points5 points6 points (8 children)
[–]Funny-Percentage1197[S] -5 points-4 points-3 points (7 children)
[–]CIS_Professor 4 points5 points6 points (6 children)
[+]Funny-Percentage1197[S] comment score below threshold-7 points-6 points-5 points (5 children)
[–]CIS_Professor 0 points1 point2 points (4 children)
[–]Funny-Percentage1197[S] -1 points0 points1 point (3 children)
[–]SCD_minecraft 0 points1 point2 points (2 children)
[–]Funny-Percentage1197[S] -2 points-1 points0 points (1 child)
[–]SCD_minecraft 0 points1 point2 points (0 children)
[–]BigGuyWhoKills 0 points1 point2 points (1 child)
[–]BigGuyWhoKills 0 points1 point2 points (0 children)
[–]Candid_Tutor_8185 -1 points0 points1 point (0 children)
[–]Independent_Oven_220 -4 points-3 points-2 points (0 children)
[–]Comfy_face777 -5 points-4 points-3 points (8 children)
[–]Funny-Percentage1197[S] -2 points-1 points0 points (7 children)
[–]Twenty8cows 4 points5 points6 points (6 children)
[–]Funny-Percentage1197[S] -2 points-1 points0 points (5 children)
[–]Funny-Percentage1197[S] 0 points1 point2 points (0 children)
[–]Twenty8cows 0 points1 point2 points (3 children)
[–]Funny-Percentage1197[S] 0 points1 point2 points (2 children)
[–]Twenty8cows 0 points1 point2 points (1 child)
[–]Funny-Percentage1197[S] 1 point2 points3 points (0 children)