Python Forum

Full Version: what is wrong with this 'if' statement!?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello!
I am new to coding and I have 0 experience.
I've started learning just yesterday! I was writing something to take a full name and sort out first and last name. but I have a problem.


while True:
    full_name = input('What is your full name!? ')
    first_and_last_name = full_name.split(' ')
    first_name = first_and_last_name[0]
    number_of_name_segments = len(first_and_last_name)
    full_name_length = len(full_name)
    numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    numbers_in_name = False
    for letters in full_name:
        if letters in numbers:
            numbers_in_name = True
            break
    if numbers_in_name:
        print('Names can\'t have numbers in them')
    if full_name_length < 6:
        print('Your name is too short.')
    if full_name_length > 50:
        print('Your name is too long.')
    if number_of_name_segments <= 1:
        print('Please enter your full name.')
    if number_of_name_segments == 2:
        last_name = first_and_last_name[-1]
        middle_name = 'none'
        break
    if number_of_name_segments == 3:
        last_name = first_and_last_name[-1]
        middle_name = first_and_last_name[1]
        break
    if number_of_name_segments > 3:
        print('Only enter your first and middle and last name.')
print(f'Hello {first_name} {middle_name} {last_name} welcome abroad!')
this is my code. the problem is when I enter numbers instead of my name the code doesn't go back to the top. it just accepts the numbers as a name!!!
but if I put less than 6 characters it won't accept and it will go back to the top!
why is that?!what is the difference between these two lines of code!?
Thank you Tongue please help me learn Big Grin
I think you are misinterpreting what is happening.

Your first 4 tests (between lines 13 and 20) don't do anything except print some data to the screen. They don't stop the program or affect it. You'd need to have a break or some other logic for them to do that.

The only tests that stop the loop are the next 2 (number of segments is 2 or 3). Everything else is just a warning. So putting in less than 6 characters is still accepted, as long as there are 2 or 3 segments.