How do i prevent a user from entering an empty string? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do i prevent a user from entering an empty string?

Code: names = [ ] num_of_invites = int(input("how many invites: " )) while True: for name in names: if len(names) < num_of_invites: per_invite = input("name of invite: ") names.append(per_invite) ~snip~ Problem: I have a problem when the user decides to enter a "ghost" invite(empty string), which will eventually be reflected in the names list.

4th Mar 2022, 6:33 PM
Chris
4 Answers
+ 5
If it is empty then ask again by a loop. Else take a default value.. edit: Chris per_invite = input("name of invite:\n") or "chris" print(per_invite) or like while True : per_invite = input("name of invite:\n") if per_invite : break print(per_invite) edit: #it just about taking the input name. you can adjust to your code without extra loop as you going it loop already.. hope it helps....
4th Mar 2022, 6:44 PM
Jayakrishna 🇮🇳
+ 4
num = int(input() or "42")
4th Mar 2022, 6:46 PM
Oma Falk
Oma Falk - avatar
+ 3
Another attempt could ve using a try-block: try: x = int(input()) except: x = 99
4th Mar 2022, 7:01 PM
Lisa
Lisa - avatar
+ 3
Why are you using `while True:` when you already know how many people are to be invited from <num_of_invites>?
5th Mar 2022, 1:49 AM
Ipang