I need a help with the RETURN function

Hi guys! I was trying to make this small project to understand better how comparison operators works. I have the following problem: I want the program to ask the user’s name, if it has less than 3 characters, program will show an error message, if it has more than 50 characters, it will show another error message.

I want the program to return the user to the first line (as you can see in this image):

BUT I don’t know how to do it. I was trying to find the answer on Saint Google but it was difficult.

Should I used the return function to return the user to the first line until he write a name with more than 3 and less than 50 characters?

name = input('Your name: ')

if len(name) < 3: 
	print('Name must be at least 3 characters')
elif len(name) > 50:
	print('Name must be a maximum of 50 characters')
else:
	print('Name looks good!')
	
#How can I do to make the program returns the user to first line if the name's characters are less than 3 or greater than 50?

Thanks!

Most definitely the place for a loop.

while True:
  if len(name) < 3: 
    print('...')
  elif len(name) > 50:
    print('..')
  else:
    print('Name looks good!')
    break
2 Likes

Oop! Be sure to include the input() inside the loop. My bad.

1 Like

HAHAHAHAHAHA oh, you won’t believe it. I was nearly half an hour trying to find WhY i’m having a bug :confused:

2 Likes

I set the “input” outside the while loop

Still not working!


I’m trying to find out how can I make the program to return to the input and insist to the user to write a valid name

Write the input statement after the while.

while True:
  name = input('...')
  if len(name) < 3: 
    print('...')
  elif len(name) > 50:
    print('..')
  else:
    print('Name looks good!')
    break

it keeps giving an error :frowning:

Don’t change the while condition, True.

1 Like

You are right!
.
.
.

1 Like

FINALLY I GOT IT!

Thanks for the help!

while True:
	name = input('Your name:')
	if len(name) < 3:
		print('Name must be at least 3 characters long')
	elif len(name) > 50:
		print('Name must be a maximum of 50 characters')
	else:
		print('Name looks good!')
		break

1 Like