+ 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.
4 Réponses
+ 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....
+ 4
num = int(input() or "42")
+ 3
Another attempt could ve using a try-block:
try:
x = int(input())
except:
x = 99
+ 3
Why are you using `while True:` when you already know how many people are to be invited from <num_of_invites>?