Charm python------if-else statement

Source: Internet
Author: User

Introduction: The role of If-else to meet a condition to do what, otherwise do what.

If-else statement Syntax Structure

if Judging Condition:

The code to execute

Else :

The code to execute

Judging condition: Generally a value of a relational expression or bool type

Execution procedure: The program runs to if, first to determine the conditions, if the condition is true, is the return value is False, the following code is executed, if the condition is not true, then the return value is the following code to continue execution.

Example 1 : Impersonate a user login

Prompt to enter user name and password

If the user name is Admin , the password equals 123.com, prompting the user to log on successfully

If the user name is not Admin that indicates that the user does not exist

If the password is not equal to 123.com, Prompt for password error

Username= input (" Please enter user name:")

Password = input (" Please enter password:")

if username.lower (). Strip () = = "admin " and password = = "123.com":

print (" login Successful! ")

Else:

print (" user name or password is wrong!") ")

# lower ()-- Convert a string to lowercase Upper ()-- Convert a string to uppercase

# Strip ()-- remove spaces before and after a string

Results:

c:\python\python.exec:/python/demo/file2.py

Please enter user name: ADMIN

Please enter the password: 123.com

Login Successful!

Process finished with exit code 0

Example 2 :

Enter Xiao Wang (Chinese, English, maths) score in the console application (100 points in a single branch)

Judge:

1) If all subjects have passed, please note: Congratulations, all your subjects have passed the exam.

2) Otherwise remind: Unfortunately, you did not pass the exam, need to retake (not the name of the pass)

chinese= Int (input (" Please enter language score:"))

Maths = int (input (" Please enter Math score:"))

中文版 = int (input (" Please enter English score:"))

Get_course = ""

if chinese>= and Maths >= and c19>english>= :

print (" Congratulations, all subjects pass the exam!") ")

Else:

if Chinese <= :

Get_course + = " Chinese,"

If maths <= :

Get_course + = " math,"

If 中文版 <= :

Get_course + = " English,"

Print (" Unfortunately, you did not pass the exam, the retake account is:" + get_course)

Results:

c:\python\python.exec:/python/demo/file2.py

Please input language score: 54

Please enter Math score: 89

Please input English score: 32

It is a pity that you did not pass the exam, the subject of the retake is: Chinese, English,

Process finished with exit code 0

Example 3: (Before the small safflower case for the first time optimization)

Enter Xiao Wang (Chinese, English, maths) score in the console application (100 points in a single branch)

Judge:

1) If one door is 100 points

2) If there are two doors greater than 90 points

3) If you have more than 80 points

To satisfy one of these conditions, a small red flower is rewarded

chinese= Int (input (" Please enter language score:"))

Maths = int (input (" Please enter Math score:"))

englist = int (input (" Please enter English score:"))

Get_course = ""

if  (Chinese = = or maths = = or englist = = ):

if(Chinese = = ): Get_course + = " Chinese,"

if (maths = =): Get_course + = "math,"

if (englist = =): Get_course + = "English,"

print ("Your%s has scored a bonus for a small red flower?") ! " % get_course)

Else:

&NBSP; if (Chinese 90&NBSP; and< Span class= "Apple-converted-space" >&NBSP; maths >=90) or&NBSP; (Chinese  >= 90&NBSP; and&NBSP; englist>=  90) &NBSP; or (Maths >= and&NBSP; englist >= 90):

if (Chinese >= ): Get_course + = " Chinese,"

If(Maths >= ): Get_course + = " math,"

If(englist >= ): Get_course + = " English,"

Print (" your %s is greater than the score to reward a small red flower ? ") ! " % get_course)

Else:

if (Chinese >= and maths >=80 and Englist >=< c21>):

Print (" your three subjects Chinese, math, English are more than ten points, reward a small red flower ? ") ")

Results:

c:\python\python.exec:/python/demo/file2.py

Please input language score: 86

Please enter Math score: 98

Please input English score: 87

Your three subjects Chinese, maths, English are more than 80 points, reward a small red flower?

Process finished with exit code 0

Charm python------if-else statement

Related Article

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.