+ 2

Can you, please, comment on this?

Hi, do you think this is professional? If not, please, tell me a proper way of getting input using function and also havning exceptions handled. def get_username(): counter = 0 # keep track of recursive steps username = None # string to be returned try: temp = input("Username: ") assert not temp[0].isdigit() #check so the first character is not a digit print("-|Input Complete!") username = temp # if this line is executed input is complete, setting value except AssertionError: print("-|Username can't start with a number!") print("-|Input Failed!") print("---------------") counter += 1 #incrementing step username = get_username() # recursive call finally: if(counter == 0): #Used this part to avoid $counter$ times repeat of this line print("-|Processing...") #if(counter == 0): print("-|Processing...") return username

15th Feb 2020, 6:51 AM
Temp Name
Temp Name - avatar
4 Respuestas
+ 5
Hello. I suggest next time post your code bits on the playground and attach to your question. I think your approach is fine, although I probably wouldn't use recursion for this, more like an infinite while loop. When there is no exception, you can return the result from within the try block. You might want to add other assertions too, for example the username cannot be empty (in this case you currently get IndexError) or can't contain whitespaces or special characters. Using a regexp is handy in these cases. Here is a refactored version of your code. https://code.sololearn.com/cSNku0LjKqZ0/?ref=app
15th Feb 2020, 7:26 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Thank you so much. I am sorry, first time posting here. Will, definitely add more assertions. ^^
15th Feb 2020, 7:40 AM
Temp Name
Temp Name - avatar
+ 1
Code Crasher thanks, I am glad my example was helpful. Your additional assertion looks ok, but it seems you are trying to combine the while loop with the recursive call, you wouldn't need both. And the 'finally' clause is really not needed here. We typically use it when we want to make sure that some resource we have opened, like a file or database, is closed properly at the end, regardless if there was an exception or not. I added a recursive version to my code so you can try it. Here is it: # recursive version def get_username_recursive(): try: username = input("Username: ") assert not username[0].isdigit() print("-|Input Complete!") return username except AssertionError: print("-|Input Failed!") return get_username_recursive() The last line is the key, because when you make a recursive function, the control flow will eventually come back to the place from where you recurse. Username returned, will propagate back to each recursive call.
15th Feb 2020, 12:57 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Code Crasher I'm happy for your progress ;) Your logic about the indexerror is almost right, only a slight technical detail is that input() will always return a string type object - which may be an empty string with 0 length, if the user does not enter anything. So yes in this case username[0] tries to access the first character that doesn't exist. No need to panic about SoloLearn, it's just how the input works here in non-interactive way. You have to enter all inputs for the program upfront in the same dialog box, in separate lines, like: 1wronginput correctusername You can play this code that explains in detail https://code.sololearn.com/WhiNb9BkJUVC/?ref=app
16th Feb 2020, 4:23 AM
Tibor Santa
Tibor Santa - avatar