Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use one else statement with multiple if statements

Tags:

python

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.

like image 710
superepicprogrammer Avatar asked Oct 06 '21 23:10

superepicprogrammer


People also ask

Can multiple if statements have one else?

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 {} . .

How do you use multiple IF and ELSE?

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.

How do you write an if statement with multiple conditions?

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)

Can you have 2 if statements in a row?

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.

How do you use the else part of an if/else statement?

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.

How to check multiple IF statements in Excel?

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.

How to use the if statement in Excel?

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

How many if statements can you have in an Excel formula?

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.


Video Answer


4 Answers

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.

like image 69
Grismar Avatar answered Oct 18 '22 18:10

Grismar


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])
like image 3
bituniverse Avatar answered Oct 18 '22 19:10

bituniverse


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!")
like image 1
Code-Apprentice Avatar answered Oct 18 '22 19:10

Code-Apprentice


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"]))
like image 1
Samwise Avatar answered Oct 18 '22 17:10

Samwise