So I'm trying to make a login prompt and I want it to print 'Success' only if there are no errors. This is the code I'm using:
if not is_email(email) or not is_name(name) or password != confirmPassword or not is_secure(password):
if not is_email(email):
print('Not a valid email')
if not is_name(name):
print('Not a valid name')
if password != confirmPassword:
print('Passwords don\'t match')
if not is_secure(password):
print('Password is not secure')
else:
print('Success')
Would there be any way to make this code shorter? I want to make it show all the errors at once so I'm not using elif.
If you have more than one condition, you can add other condition with else if. So, if first condition false and second condition is true, then the second condition will be run. If condition1 {} If condition2 {} . .
Curly brackets with if/else statements If you want to execute multiple statements for the else condition, enclose the code in curly brackets. If you only need to execute a single statement for the else condition, you do not need to use curly brackets.
When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)
The IF function allows you to make a logical comparison between a value and what you expect by testing for a condition and returning a result if True or False. So an IF statement can have two results.
The else part of the if/else statement follows the same rules as the if part. If you want to execute multiple statements for the else condition, enclose the code in curly brackets. If you only need to execute a single statement for the else condition, you do not need to use curly brackets.
There are various ways to apply the If statement. Here you can check multiple if statements in excel using Nested Ifs and Logical test. In Excel, there are many ways to use If statements. Here we are going to describe all if the functions that one can use to test more than one condition.
Excel If Statement 1 Let’s take a data set which is shown above. 2 Here we use Excel If contains data set to check whether the student is Pass or fail based on his or her scores. 3 To perform this, we apply Excel if formula. The syntax of this formula is
You can use up to 64 IF statements in a formula in Excel, but you probably shouldn't since it becomes increasingly difficult to make sure your results are accurate. Open your project in Excel. If you're in Excel, you can go to File > Open or you can right-click the file in your file browser.
A way to avoid repeating the tests:
ok = True
if not is_email(email):
print('Not a valid email')
ok = False
if not is_name(name):
print('Not a valid name')
ok = False
if password != confirmPassword:
print('Passwords don\'t match')
ok = False
if not is_secure(password):
print('Password is not secure')
ok = False
if ok:
print('Success')
It's not shorter, but it's clear and saves the extra comparisons, so it's faster and not as wasteful.
How about this?
flags = [is_email(email), is_name(name), password != confirmPassword, is_secure(password)]
prints = ['Not a valid email', 'Not a valid name', 'Passwords don\'t match', 'Password is not secure']
for index in range(len(flags)):
if flags[index] == False:
print(prints[index])
One way is to use a flag:
has_errors = False
if not is_email(email):
print(...)
has_errors = True
...
if not has_errors:
print("Success!")
Another approach: accumulate the errors into a list, and then you can check the error list for truthiness instead of re-checking all the error conditions. This also saves you from having to maintain another piece of state and have another line of code in each if
block to update it.
errors = []
if not is_email(email):
errors.append('Not a valid email')
if not is_name(name):
errors.append('Not a valid name')
if password != confirmPassword:
errors.append('Passwords don\'t match')
if not is_secure(password):
errors.append('Password is not secure')
print("\n".join(errors or ["Success"]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With