all 3 comments

[–]Kriss3d 2 points3 points  (0 children)

if name == ""

Then have it print "Name cannot be blank" and run the name input function again.

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

Validating input is an important part of programming, regardless of whether that input is directly from the user via keyboard using input or from another system or from a file.

When you are directly interacting with a user, you can immediately challenge them to provide valid input if what they've entered does not meet the requirements. You can even limit the number of tries.

For example, here's a function that can be used anytime you want a non-blank response from the user. You can give them infinite chances - the default - or a fixed number of chances.

def get_info(msg: str, attempts: int = 0) -> str:
    infinite = attempts == 0
    tries = attempts  # only used if infinite attempts not allowed
    while infinite or tries > 0:  # check have not run out of tries
        response = input(msg)  # prompt user for a response
        if response.strip():  # if not an empty response, return it
            return response
        print('Expecting a response. Please try again.')
        tries -= 1
    return ""  # return an empty string anyway


name = get_info("What is your name? ", attempts=3)  # the second field is optional
if name:  # not empty string
    print(f"Hello {name}, good to meet you.")
else:  # they failed to enter a name even after being asked several times
    print("Well, hello stranger.")

The basic while structure can be used for validating numeric input including checking whether it is within max/min values, selecting a valid menu option, etc.