+ 2

Taking input multiple times in solo learn. How to make this shorter

https://code.sololearn.com/cBObh8gBKkR8/?ref=app My program checks if the password you have selected is valid if not it requires you to input password again. In a separate ide I was using a while loop but the input method is not being recognized in solo learn how can I get around this Another question is how can I make this shorter. I am new to python so I usually end up writing longer code when there is a shorter way of doing it

19th Jan 2023, 8:19 AM
Sami Yemane
Sami Yemane - avatar
13 Antworten
+ 7
Sololearn input is not interactive. It needs all required inputs at once before start execution. So just enter all inputs by line by line separated in the pop-up then hit enter. Ex: ABC <press enter> Abcde <press enter> Abc1 <press hit> You can use letter.isdigit() method to check if it is digit or not. So no need list of numbers and no need inner loop.. You can return true then just. Otherwise return false, can be reduce furthermore...
19th Jan 2023, 9:00 AM
Jayakrishna 🇮🇳
+ 10
Sami Yemane , this is an approach that uses a for loop and the string method isdigit(). it does not need a counter, since we will break in case of a digit is detected when iterating on the input string. then return value is True. if there is no digit detected, the loop will terminate, but by using an else clause. then return value is False. you should adapt your main program to get the return value, and then select the respective output. def seperator(word): for char in word: if char.isdigit(): return True else: return False ... call of function ...
19th Jan 2023, 11:51 AM
Lothar
Lothar - avatar
+ 4
Furthermore you could use the so-called "comprehensions" to reduce the whole loop to a oneliner: while True: if any(letter.isdigit() for letter in password): break
19th Jan 2023, 11:54 AM
HonFu
HonFu - avatar
+ 3
For Python codes on Sololearn, it's easiest to refrain from using infinite loops and just reduce it to a single input. It may not be the best or realistic way, but that's just the way it is here with console-based codes. So basically like: x = input() if x == whatever: print('Yay!') else: print('Nay.')
19th Jan 2023, 12:15 PM
HonFu
HonFu - avatar
+ 1
HonFu that is an interesting solution I will try it out
19th Jan 2023, 11:59 AM
Sami Yemane
Sami Yemane - avatar
+ 1
Jayakrishna🇮🇳 thanks that sucks tho for what I am trying to do Does the isdigit() return true if the argument is an int
19th Jan 2023, 12:01 PM
Sami Yemane
Sami Yemane - avatar
+ 1
Thanks guys I have just tried it I get what it does now
19th Jan 2023, 12:05 PM
Sami Yemane
Sami Yemane - avatar
+ 1
Sami Yemane, isdigit is a method of the string datatype, so it works directly on the string (your input) and doesn't need a conversion to int.
19th Jan 2023, 12:12 PM
HonFu
HonFu - avatar
+ 1
HonFu thanks
19th Jan 2023, 12:17 PM
Sami Yemane
Sami Yemane - avatar
+ 1
print("012345789".isdigit()) # true print("abc1".isdigit()) #false # isdigit() method of str class returns true, if the all characters are digits in string else false.
19th Jan 2023, 12:25 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 Thanks got it
19th Jan 2023, 12:26 PM
Sami Yemane
Sami Yemane - avatar
0
Lothar does the isdigit only work of the input is a int because I tried getting the type and it was a string
19th Jan 2023, 11:59 AM
Sami Yemane
Sami Yemane - avatar
0
Sami Yemane for continuous input, you can use loop. For eg.- i=0 while i<10: password_input=input() And if you want to match password: use re module import re pattern=r"[0-9]+
21st Jan 2023, 4:48 AM
viswajeet
viswajeet - avatar