+ 1
Question about python task
try: name = input() #ваш код if len(name)>=4: print("Account Created") except: print("Invalid Name") #not worked #why?
17 ответов
+ 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
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")
+ 4
else is missing in your code!
+ 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
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 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.