Reputation: 714
I'm trying to set up a block to accept only inputs that are in a list but first it asks for the inputs in the input function but I can't seem to get rid of the quotes around the strings in the list. Here is some example code:
def Sinput(acceptable):
while True:
acceptable = [str(i) for i in acceptable]
a = input('Enter'+str(acceptable[:-1]).strip('[]')+' or '+str(acceptable[-1]+': '))
if a in acceptable:
return a
break
a = Sinput([ 1, 2.01, '\'cat\'', 'dog'])
print('you entred:', a)
The input asks: Enter'1', '2.01', "'cat'" or dog:
I want it to ask: Enter 1, 2.01, 'cat' or dog:
Using .replace('\'', '')
won't work because the input 'cat' would no longer display correctly
Thanks for any help, I've only been doing coding for about a week.
Upvotes: 1
Views: 1553
Reputation: 27889
I think this would do good for you:
a = input('Enter {} or {}'.format(' ,'.join(acceptable[:-1]), acceptable[-1]))
Upvotes: 2
Reputation: 78554
Use .join(...)
which is the recommended way for joining an iterable of strings:
a = input('Enter'+ ' ,'.join(acceptable[:-1]) + ...)
# ^^^^^^^^^
P.S. I don't see why you need a break
after that return
statement.
Upvotes: 3