How to make that the code below doesn`t accept less then two letters? | Sololearn: Learn to code for FREE!

+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!

2/29/2020 1:06:20 PM

Stanislav Voloshchuk

10 Answers

New Answer

+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

I don't know what a symbol is in your language, to me..they're special characters like $,,#,%,@ Is that what you're talking bout ?

+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

The max I can do, though you're question isn't clear enough even after you edited it...I'll suggest you add more to the description and not the question a = input() if len(a) >= 1 and len(a) <= 2: print("Requirement met") else: print("Doesnt meet the requirement")

+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

I think Ik what you want now https://code.sololearn.com/c3uQUHqmYp10/?ref=app

+1

andriy kan Thanks! Mirielleđź‘˝ Thank for your example as well!