Functions and sample code of the if else statement in python

Source: Internet
Author: User

Functions and sample code of the if else statement in python

Introduction: if-else is used to determine what to do if one condition is met, or else what to do.

If-else statement syntax structure

If condition:

Code to be executed

Else:

Code to be executed

Condition: It is generally a relational expression or a bool value.

Execution Process: when the program runs at the if clause, it first determines the condition. if the condition is True, the return value is True, and the following code is executed. if the condition is not True, the return value is False, continue to execute the following code.

Example 1: Simulate User Logon

Prompt for username and password

If the user name is Admin and the password is 123.com, a message indicating successful logon is displayed.

If the user name is not Admin, the system prompts that the user does not exist.

If the password is not 123.com, a Password error is prompted.

Username = input ("enter username:") password = input ("Enter password:") if username. lower (). strip () = "admin" and password = "123.com": print (" Login successful! ") Else: print (" incorrect user name or password! ") # Lower () -- convert string to lowercase upper () -- convert string to uppercase # strip () -- remove spaces before and after string

Result:

C: \ python \ python.exe C:/python/demo/file2.py

Enter the User name: ADMIN

Enter the password 123.com

Login successful!

Process finished with exit code 0

Example 2:

Enter Mr. Wang (Chinese, English, and mathematics) score in the console application (out of 100 for a single subject)

Judgment:

1) if all the subjects pass, the prompt is: Congratulations, you have passed the test for all the subjects.

2) otherwise, you will be reminded that, unfortunately, you have not passed the exam and you need to make up the exam (no passing name)

Chinese = int (input ("Enter the chinese score:") maths = int (input ("Enter the math score :")) english = int (input ("Enter the english score:") get_course = "" if chinese >=60 and maths >=60 and english >=60: print ("congratulations, all subjects passed the test! ") Else: if chinese <= 60: get_course + =" chinese, "if maths <= 60: get_course + =" mathematics, "if english <= 60: get_course + = "English," print ("sorry, you did not pass the test. The retake subject is:" + get_course)

Result:

C: \ python \ python.exe C:/python/demo/file2.py

Enter the language score: 54

Enter the math score: 89

Enter English score: 32

Unfortunately, you did not pass the test. The subjects for retake exams are: Chinese, English,

Process finished with exit code 0

Example 3: (the first optimization of the small safflower case)

Enter Mr. Wang (Chinese, English, and mathematics) score in the console application (out of 100 for a single subject)

Judgment:

1) if one course is 100 points

2) if there are two doors greater than 90 points

3) if the number of the three doors is greater than 80

To meet the above conditions, reward a small red flower

Chinese = int (input ("Enter the chinese score:") maths = int (input ("Enter the math score :")) engist = int (input ("Enter the English score:") get_course = "" if (chinese = 100 or maths = 100 or engist = 100 ): if (chinese = 100): get_course + = "language," if (maths = 100): get_course + = "mathematics," if (englist = 100 ): get_course + = "English," print ("Your % s scored 100 points and rewarded a small red flower! "% Get_course) else: if (chinese> = 90 and maths> = 90) or (chinese> = 90 and englist> = 90) or (maths> = 90and englist> = 90): if (chinese> = 100): get_course + = "language," if (maths> = 90 ): get_course + = "mathematics," if (englist> = 90): get_course + = "English," print ("Your % s is greater than 90 points, reward a small red flower success! "% Get_course) else: if (chinese> = 80 and maths> = 80 and engist> = 80 ): print ("your three subjects are more than 80 points in Chinese, mathematics, and English, and you will be rewarded with a small safflower ")

Result:

C: \ python \ python.exe C:/python/demo/file2.py

Enter Chinese score: 86

Enter the math score: 98

Enter English score: 87

Your three subjects, including Chinese, mathematics, and English, all scored more than 80 points.

Summary

The above section describes the functions and sample code of the if else statement in python. I hope it will be helpful to you. if you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.