Python Forum
Trying to create a conditional with a username
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to create a conditional with a username
#1
hi guys ~

i need help trying to code a name question but im trying to repeat the "enter your name" if the user just press enter without type nothing


print('Welcome')

user_name = (input('Please enter your name:'))

if user_name  == input(''):
    print(f'Hi {user_name}! im here to help you')
else: 
    print('Please enter a name')
thanks for the help
Reply
#2
Note in your line 5, you're not testing if the user entered a blank name, you're testing whether it is the same as a separate input request (which is blank). That's not what you want, and it's confusing to the user.

Create some sort of loop that you stay in until your request is satisfied.

while True:
    user_name = input('Please enter your name: ')
    if user_name == '':
        print("I'm sorry, I don't see your name.  Let's try again.")
    # Could do other validation here besides just blank name.
    else:
        break

print(f"Pleased to meet you {user_name}.  Let's get started.")
Reply
#3
thanks for answerd, i fixd it! im new at programming, this helps me a lot to learn
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,663 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Hiding username and password on sql tantony 10 2,889 Oct-21-2022, 07:58 PM
Last Post: wavic
  Pulling username from Tuple pajd 21 3,333 Oct-07-2022, 01:33 PM
Last Post: pajd
  Client OS username ImPyBoy17 5 2,687 Sep-24-2019, 10:18 AM
Last Post: buran
  creating a username and pword program using a #def statement and #dictionary zcode12 3 3,134 Oct-14-2018, 04:41 AM
Last Post: volcano63

Forum Jump:

User Panel Messages

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