0

Having issues detecting blank inputs

https://code.sololearn.com/cnsj6atyUb2A/#py How do I prevent a user from entering a blank entry for the requested input? I figured out how to prevent spaces, but a blank input is still accepted if a user just presses enter. I outlined the part of this code that blocks spaces, but it doesn't block the blank entry

19th Apr 2020, 8:05 PM
Joshua Cronin
Joshua Cronin - avatar
4 Respuestas
+ 1
When you check a string for True or False, it is True if there's *anything* in it (even a space), and False, if there's nothing in it. input, if the user just hits enter, gives an empty string. And you can just test for that. inp = input() if not inp: print('What, no input?')
19th Apr 2020, 8:39 PM
HonFu
HonFu - avatar
+ 1
Generally (not on Sololearn) I'd take input in a while loop, then break if all my conditions are met. while True: inp = input() if ...: break To check if a str is empty, you can just use 'not inp' - str is False when it is of zero length.
19th Apr 2020, 8:24 PM
HonFu
HonFu - avatar
+ 1
Please explain that line. "not inp" -str is False Why the negative sign in front of str and how does this accomplish the zero length? Ive tried len to no success and I am confused as to why this would work. Thank you very much 👍 Not trying to be difficult just really dont understand that line
19th Apr 2020, 8:28 PM
Joshua Cronin
Joshua Cronin - avatar
0
Ah. I see what you mean. Thank you kind one! I guess I was making this way more complicated than it had to be haha. EDIT I'm going to have to research this a bit I think
19th Apr 2020, 8:42 PM
Joshua Cronin
Joshua Cronin - avatar