Python Conditional Control

Python Conditional Control, conditional control is actually the use of if...else. Let’s first look at the basic structure of conditionals:
Python Conditionals

To summarize the function of the if…else structure in one sentence: If the condition… is true, do…; otherwise, do…

A condition is a condition that is true, that is, a Boolean expression that returns a value of True. Once you understand this, using it shouldn’t be difficult.

Let’s create a function based on the concept of functions and analyze its principles line by line:

def account_login():
password = input('Password:')
if password == '12345':
print('Login success!')
else:
print('Wrong password or invalid input!')
account_login()
account_login()
  • Line 1: Defines the function, which requires no parameters;
  • Line 2: Uses input to obtain the user input string and stores it in the variable password;
  • Lines 3 and 4: Set the condition: If the user input string is equal to the preset password 12345, print the text ‘Login success!’ ’;
  • Lines 5 and 6: Conversely, any input result that is not equal to the preset password will print an error message and call the function again, prompting the user to enter the password again;

  • Line 7: Run the function;

  • Line 8: Call the function.

It’s worth noting that if the Boolean expression following the if statement is too long or difficult to understand, you can assign a variable to store the Boolean value True or False returned by the Boolean expression. Therefore, the above code can be written like this:

def account_login():
password = input('Password:')
password_correct = password == '12345' #HERE!
if password_correct:
print('Login success!')
else:
print('Wrong password or invalid input!')
account_login()
account_login()

Generally, when designing a program, it’s important to consider logical integrity and prevent potential user frustration. This is where multiple conditionals come into play.

Multiple conditionals are also simple. Simply add elif between the if and else statements. The usage is the same as with if. The conditions are evaluated sequentially. First, check whether the condition is true. If so, execute the following code. If not, check whether the following condition is true. If none of them are true, execute the corresponding else statement.
Python Conditional Control

Next, we use the elif statement to add a password reset function to the function we just designed:

password_list = ['*#*#','12345']
def account_login():
password = input('Password:')
password_correct = password == password_list[-1]
password_reset = password == password_list[0]
if password_correct:
print('Login success!')
elif password_reset:
new_password = input('Enter a new password:')
password_list.append(new_password)
print('Your password has changed successfully!') 
account_login() 
else: 
print('Wrong password or invalid input!') 
account_login() 
account_login()
  • Line 1: Creates a list to store the user’s password, initial password, and other data (a simplified simulation of an actual database);
  • Line 2: Defines a function;

  • Line 3: Uses input to obtain the string entered by the user and stores it in the variable password

  • Line 4: When the password entered by the user matches the last element in the password list (i.e., the user’s most recently set password), login is successful.

  • Lines 5-9: When the password entered by the user matches the first element in the password list (i.e., the “password” for resetting the password), a password change is triggered and the changed password is stored at the end of the list, becoming the user’s most recent password.

  • Line 10: Conversely, any input result that does not match the default password will print an error message and call the function again, prompting the user to enter the password again.

  • Line 11: Function call.

In the above code, you can clearly see code blocks. Code blocks are created by indentation. That is, code with the same indentation actually completes the same task at the same level, a bit like different levels of task lists when editing a document.

Leave a Reply

Your email address will not be published. Required fields are marked *