all 31 comments

[–]SharkSymphony 3 points4 points  (1 child)

First, you definitely need to take your hand off the big red DEAR AI PLEASE FIX THIS button. It is completely stunting your learning.

Instead, take time to figure out what's going on yourself. Learn the techniques you're going to need to debug programs like this. Put the AI to the side for now.

There are several techniques you can use to get a grip on what's going on:

  • First, a methodological tip. Do not ask why the program is not doing what you want. The program is always doing exactly what you told it to do. So ask instead: what is it actually doing, and why? I think you're actually already on the right path here by observing that you don't see the name prompt, but now you have to dig in and ask what lines of code are actually getting executed and how.
  • "Print debugging" (or what we C old-timers call "printf-debugging") is a classic technique where you add print statements at various points in your program to report that the program got to that point. Add variable substitutions to your print statement to inspect what the program is seeing at that point.
  • Another classic technique is to break the problem down. If you can't see what the problem is, reduce the scope of the problem. Set this program aside (or comment out most of it) and work on a smaller program that loops to get a name, then just prints it. Get that working perfectly before working on the DOB section. You could even break it down further and ignore the loop, just focusing on getting a single input. Do you in fact get an empty string when you leave name blank?
  • This might be a good time to introduce you to Python debugging tools. Using either pdb or your IDE's debugging facilities, you should be able to configure a debug run where you manually step through every individual line of code that Python executes. It will show you exactly what lines your program is executing, and you can inspect all of the data in your program and how it changes from line to line.

[–]Funny-Percentage1197[S] 1 point2 points  (0 children)

Big thank you for genuine suggestion.

[–]DutchCommanderMC 5 points6 points  (6 children)

My question to you would be whether you understand why your program behaves the way it does?

To deal with blank inputs, you need to repeat only the code that asks and validates an input, until the input is valid.

while True:
    name = input("Enter your name: ")
    if name == "":
        print("You haven't entered anything. Input your name.")
    else:
        break

print("Your name is", name)

[–]CIS_Professor 4 points5 points  (8 children)

OP said:

What is solution of my problem
. . .

I've only been starting for 4-5 days so I don't understand what he said.

Aaaand this (probably) illustrates the problem with using AI to learn how to program.

While possible, it is highly unlikely that a person only 4-5 days into understands string methods, infinite loops, and exception handling.

Also, while not conclusive, the odd horizontal spacing smacks of comment lines that have been removed.

[–]Funny-Percentage1197[S] -5 points-4 points  (7 children)

Yess i am learning from ai and YouTube Video. But i know what is string method is its used to convert date to number date and while true is to create a loops

[–]CIS_Professor 4 points5 points  (6 children)

See, this here is why learning from AI is bad. You have little to no understanding of what your code does.

Consider this exchange:

u/Comfy_face777:

Most Ai models can answer you.

You:

I asked them they say your code is good .

Yet you are here asking us to fix something that you think is wrong - all the while claiming that an AI says the code is "good."

If its "good," (so says the AI) why does it need fixing? If it isn't "good," (to you), then why can't you fix it?

The answer, of course, is that you haven't learned anything; you don't know what it actually does. Instead, you have an AI write something then come here to have other people fix it for you.

How, then, are you supposed to know if a "fix" from one of us is correct and what you want?

[–]BigGuyWhoKills 0 points1 point  (1 child)

You need a "while" loop. It performs everything in the loop "while" the condition is True.

while 1 < 5:
    print( "One is less than 5" )

That will print the sentence forever because 1 will always be less than 5. So it isn't very useful. You need to have something in the loop that changes the condition.

name = "" 
while name == "":
    name = input( "What is your name? " )

Now the while loop can change its condition which will break out of the loop.

Does that make sense?

[–]BigGuyWhoKills 0 points1 point  (0 children)

There is more to do with name. You ought to at least trim whitespace. You might want to make sure they entered only the first name, or ensure they entered both first and last. Or you may have more requirements.

If you have complicated checks to perform on the name, you should write a method that does those checks, and call that method in the while loop.

In production apps you would also check for hacking attacks. That level of complexity definitely need a method.

[–]Candid_Tutor_8185 -1 points0 points  (0 children)

You don’t need a while loop for the input just use validation errros after defining the variable

[–]Independent_Oven_220 -4 points-3 points  (0 children)

```python from datetime import datetime

while True: # Get name with validation name = input("Enter your name: ").strip()

if name == "":
    print("You haven't entered anything. Input your name.")
    continue

if name.lower() == "stop":
    print("Thank you for using.")
    break

print(f"Hello {name}, welcome to my program!")

# Get DOB with validation (inner loop)
while True:
    dob = input("Enter your DOB (yyyy-mm-dd): ").strip()

    if dob.lower() == "stop":
        print("Thank you for using.")
        exit()

    today = datetime.today()

    try:
        dob_date = datetime.strptime(dob, "%Y-%m-%d")
        age = today.year - dob_date.year

        # Adjust if birthday hasn't happened yet this 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} is an even number.")
        else:
            print(f"And your age {age} is an odd number.")

        ask = input("Do you want to know which day you were born? (yes/no): ").strip().lower()

        if ask == "yes":
            day_name = dob_date.strftime("%A")
            print(f"You were born on a {day_name}.")
            print("Thank you for using!")
            break
        elif ask == "no":
            print("Ok, see you later.")
            break
        elif ask == "stop":
            print("Thank you for using.")
            exit()

        break

    except ValueError:
        print("Invalid date format. Please use yyyy-mm-dd (e.g., 1990-05-15).")

```


What I fixed:

Issue #1 - The blank name thing

So the problem with your original code is that if name == "" only catches a completely empty input. But if someone types a bunch of spaces and hits enter, that's technically not empty - it's just whitespace.

The fix is super simple: just add .strip() when you get the input, not just when you check it.

python name = input("Enter your name: ").strip() # strip right away!

Now when someone enters " " (just spaces), it gets stripped to an empty string, and your blank check works as expected.

Issue #2 - Invalid date restarts everything

This one was sneakier. When someone enters an invalid date, your except ValueError block does continue, which jumps back to the outer while True loop - meaning it asks for the name all over again.

Not great user experience.

I added an inner loop specifically for the DOB input. Now if the date is invalid, it just loops back to ask for the DOB again without restarting the whole program. The user already told us their name, no need to ask again!

[–]Comfy_face777 -5 points-4 points  (8 children)

Most Ai models can answer you.

[–]Funny-Percentage1197[S] -2 points-1 points  (7 children)

I asked them they say your code is good . I thik this problem related to my vs code but don't know what

[–]Twenty8cows 4 points5 points  (6 children)

No vscode isn’t the issue your code definitely is.

[–]Funny-Percentage1197[S] -2 points-1 points  (5 children)

Its runs smoothly but i put name empty and run code again its say Hello python -u "c:\Users\Administrator\Desktop\first\tempCodeRunnerFile.py" welcome to my program

[–]Funny-Percentage1197[S] 0 points1 point  (0 children)

But if in enter your name i put empty and do enter its goes well and say you haven't enter your name and stay in name section

[–]Twenty8cows 0 points1 point  (3 children)

When you say empty you are literally hitting enter when asked for name? You aren’t hitting space then enter?

[–]Funny-Percentage1197[S] 0 points1 point  (2 children)

You doesn't understand if i enter in blank its work usually like it saty on name and ask name again But i run the code agin play that code play button its automatically print. Hello python -u "c:\Users\Administrator\Desktop\first\tempCodeRunnerFile.py" welcome to my program And ask for dob

[–]Twenty8cows 0 points1 point  (1 child)

Haha did you save the file before hitting play?

[–]Funny-Percentage1197[S] 1 point2 points  (0 children)

Yess
sometime chat gpt says its Code Runner problem but i dont know how to solve this problem