Question about python task | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Question about python task

try: name = input() #ваш код if len(name)>=4: print("Account Created") except: print("Invalid Name") #not worked #why?

4th Mar 2022, 7:39 AM
Rasl
Rasl - avatar
17 Answers
+ 9
Hi, It is a mix of try/except and if/else did u want this? try: name = input() #ваш код if len(name)>=4: print("Account Created") else: print("Invalid Name") except: print("something mystrious happened")
4th Mar 2022, 7:54 AM
Oma Falk
Oma Falk - avatar
+ 5
Rishi yes, I think, you are right! It was a mix. But I didn't want to come across with assertions. I think, Rasl is still at the evincing of his journey.
4th Mar 2022, 8:36 AM
Oma Falk
Oma Falk - avatar
+ 5
Rajeev Sharma it's not always necessary to have else statement after an if statement. If statement can exist without an else. "Else is missing in your code!" is a very misleading answer as it might make the OP think that they've to use an else everytime they use an if
6th Mar 2022, 3:43 AM
Rishi
Rishi - avatar
+ 4
What do you mean? Your code works perfectly fine https://code.sololearn.com/cCPKLzON4oGP/?ref=app
4th Mar 2022, 7:41 AM
Rishi
Rishi - avatar
+ 4
You can also do it like this name = input() if len(name) >= 4: print("Account Created") else: print("Invalid Name") But, if you want to use try-except statement, try: name = input() if len(name) < 4: raise ValueError except: print("Invalid Name") else: print("Account Created")
4th Mar 2022, 8:55 AM
Simba
Simba - avatar
+ 4
else is missing in your code!
6th Mar 2022, 2:53 AM
follow ->
follow -> - avatar
+ 3
Sandeep i meant something like this: try: if(len(name)>=4): print("Account Created") else: raise ValueError #Or some other relevant exception except: print("Invalid Name") If the question was about throwing and catch exceptions, there's no point in using if else statements even though it works. Hope you understand :)
5th Mar 2022, 2:36 AM
Rishi
Rishi - avatar
+ 3
Hi Rasl, I think Simba and Rishi are right. Maybe you should try the way they explained. Otherwise, for example as Oma Falk did, "except" has no impact on your code, it is useless.
5th Mar 2022, 7:50 AM
SoloilSole
SoloilSole - avatar
+ 3
Rasl can you post your question here?
6th Mar 2022, 3:43 AM
Rishi
Rishi - avatar
+ 2
task not running(((
4th Mar 2022, 7:43 AM
Rasl
Rasl - avatar
+ 2
Oma Falk i think the exception should be thrown if it's <4, and catch it in the except block then print "Invalid name"
4th Mar 2022, 8:15 AM
Rishi
Rishi - avatar
+ 2
Thaks brothers
4th Mar 2022, 8:37 AM
Rasl
Rasl - avatar
+ 1
Rishi Exception will not be thrown in this case. if condition will always return true or false because we are taking the raw input and every character will just be a string and hence valid. I think Oma Falk code is what Rasl wanted to achieve
4th Mar 2022, 5:28 PM
Sandeep
Sandeep - avatar
+ 1
Rishi i didn't think this way if-else can be used while using exceptions(try or except) in complex nested statement but you are right, here the logic is too simple for both exception and conditional Statements to co-exist
5th Mar 2022, 3:01 AM
Sandeep
Sandeep - avatar
+ 1
try: name = input() #ваш код if len(name)>=4: print("Account Created") else: raise ValueError except: print("Invalid Name") It was my code. Worked.
6th Mar 2022, 6:04 AM
Daniil Pivovarov
Daniil Pivovarov - avatar
+ 1
Try: Name = input() Nwords = Name.split() If Nwords>=4: Print("Account created") Else: Print("invalid name")
6th Mar 2022, 6:42 AM
Yug
Yug - avatar
0
because your program would never raise Exception. Anything you input would be transformed to string which always has length.
6th Mar 2022, 12:26 AM
王书才