Question about python task | Sololearn: Learn to code for FREE!

+1

Question about python task

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

3/4/2022 7:39:56 AM

Rasl

17 Answers

New Answer

+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")

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

+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

+4

What do you mean? Your code works perfectly fine https://code.sololearn.com/cCPKLzON4oGP/?ref=app

+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")

+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 :)

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

+3

else is missing in your code!

+3

Rasl can you post your question here?

+2

task not running(((

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

+2

Thaks brothers

+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

+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

+1

try: name = input() #ваш код if len(name)>=4: print("Account Created") else: raise ValueError except: print("Invalid Name") It was my code. Worked.

+1

Try: Name = input() Nwords = Name.split() If Nwords>=4: Print("Account created") Else: Print("invalid name")

0

because your program would never raise Exception. Anything you input would be transformed to string which always has length.