+ 1
How to make that the code below doesn`t accept less then two letters?
Anybody have an idea how to make working code below: def get_str_center(input_str): if len(input_str) < 4: return input_str elif len(input_str) <= 2: return ('Please enter more then one letter') return get_str_center(input_str[1:-1]) tests = [(input('Please enter text here: '))] for test in tests: print(f"{test} -> {get_str_center(test)}") Thanks everybody!
10 Respuestas
+ 1
repost my code again (I deleted it because i thought you need to print special symbols):
def get_str_center(input_str):
if len(input_str) < 4:
return input_str
return get_str_center(input_str[1:-1])
def get_str_center_checked(input_str):
if len(input_str) <= 2:
return ('Please enter more then one letter')
return get_str_center(input_str)
tests = ['bottle', 'bottles', 'x']
for test in tests:
print(get_str_center_checked(test))
+ 2
This snippet might help
tests = "abd#@=y"
print(len([i for i in tests if not i.isalnum()])) # output 3
+ 1
You could do it while taking the input:
tests = []
while True:
inp = input()
if len(inp)>=2:
tests.append(inp)
elif not inp:
break
+ 1
no, for me, a symbol is any character, like a letter, special symbol, digit and so on.
I didn't know what your code should print. So, I guessed, when fixed it.
+ 1
Mirielle👽 Thanks I already edit the message to be more clear.
andriy kan Thanks I have a same idea about symbols expression =)
David Ashton Thanks but it seems my question wasn`t clear enough so your advice gives me number of symbols in text. While I need to keep the original output but in case if user will input only one letter return message 'Please enter more then one letter'.
HonFu Thank you for reply but your code just make infinite input.
+ 1
Mirielle👽
Here is an example:
def get_str_center(input_str):
if len(input_str) < 4:
return input_str
return get_str_center(input_str[1:-1])
tests = ['bottle', 'bottles', 'x']
for test in tests:
print(f"{test} -> {get_str_center(test)}")
So what i need is that if in test is "x" (only one letter) it will give an Error and ask user to input more then one letter.
Thanks for attention.
+ 1
andriy kan Thanks!
Mirielle👽 Thank for your example as well!