Okay, so basically I'm writing an interactive Python program. Initially I will ask the user to enter their name, but even if they don't enter anything and just press enter, the program will still continue. How do I force them to hit enter?
            Asked
            
        
        
            Active
            
        
            Viewed 137 times
        
    -2
            
            
        - 
                    how do you mean force them to hit enter, do you mean make sure they enter something other than an empty string? – Padraic Cunningham Oct 02 '14 at 18:13
1 Answers
0
            Check that the length of your variable is greater than 0.
To find the length of a string, use len(myStringVar). See the docs for more if you are curious.
To repeatedly prompt until they enter a name, you could do something like this:
user_name = ""
while len(user_name) == 0:   # While the user_name var is empty
    user_name = raw_input('Enter a user name: ')  # Prompt the user to enter a name, and assign to the user_name var
A more pythonic way of this might be:
user_name = ""
while not user_name:  # Empty strings evaluate to False
    user_name = raw_input('Enter a user name: ')
 
    
    
        Joseph
        
- 12,678
- 19
- 76
- 115
