Python Forum
what is wrong with this 'if' statement!?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what is wrong with this 'if' statement!?
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,388 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  python gives wrong string length and wrong character thienson30 2 2,944 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Put Print Statement Wrong Somehow... abrogard 6 3,278 Aug-14-2018, 08:24 PM
Last Post: abrogard

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020