The second learning python and logic to determine if statements

Determination defined

  • If the  conditions are met , in order to do something,
  • If the  conditions are not met , it is another thing to do, or do nothing

Judge sentences  also known as "branch statement," It is because of judgment, just let the program has a lot of branches

 

 if the judge sentences the basic syntax

In  Python the, IF statement  is used for determination, the following format:

if 要判断的条件:
    条件成立时,要做的事情
    ……

Note: indent your code for a  tab key, or  4  spaces -  recommended space

  • In Python development, Tab and space do not mix!

 

2.2 Statement exercise judgment - judgment Age

demand

  1. The definition of an integer variable age of record
  2. To determine whether at least 18 years of age ( > = )
  3. If the age of 18, allowed into the cafe Hi skin
# 1. 定义年龄变量
age = 18

# 2. 判断是否满 18 岁
# if 语句以及缩进部分的代码是一个完整的代码块
if age >= 18:
    print("可以进网吧嗨皮……")

# 3. 思考!- 无论条件是否满足都会执行
print("这句代码什么时候执行?")

Note :

  • if Statements, and the indented portion is a  complete block of code

2.3 else case where the processing condition is not met

In use  if when the judge can do to do things the conditions are met. If you need it  , when the conditions are not satisfied , do certain things, how to do it?

answer

elseIn the following format:

if 要判断的条件:
    条件成立时,要做的事情
    ……
else:
    条件不成立时,要做的事情
    ……

Note :

  • if And  else statements and respective indented portions together is a  complete code block

2.4 Statement exercise judgment - judgment Age improvement

demand

  1. Enter the user's age
  2. To determine whether at least 18 years of age ( > = )
  3. If the age of 18, allowed into the cafe Hi skin
  4. If you are under 18 years of age, suggesting that home homework
# 1. 输入用户年龄
age = int(input("今年多大了?"))

# 2. 判断是否满 18 岁
# if 语句以及缩进部分的代码是一个完整的语法块
if age >= 18:
    print("可以进网吧嗨皮……")
else:
    print("你还没长大,应该回家写作业!")

# 3. 思考!- 无论条件是否满足都会执行
print("这句代码什么时候执行?")

 

 logic operation

  • Program development, typically  in the judgment conditions , a plurality of determination conditions will need to
  • Only a plurality of conditions are satisfied, it is possible to execute the subsequent code, the need to use the  logical operators
  • Logical operators  can put  a plurality of conditions  in accordance with the  logic  for  the connection , it becomes  more complex conditions
  • Python is  logical operators  include: and and / or or / Non not  three kinds

and

条件1 and 条件2
  • And / and
  • Two conditions are met, return True
  • As long as there is not met, it returns False
Condition 1 Condition 2 result
Set up Set up Set up
Set up Failure Failure
Failure Set up Failure
Failure Failure Failure

or

条件1 or 条件2
  • Or / or
  • As long as there is a satisfying two conditions, return True
  • Two conditions are not satisfied, return False
Condition 1 Condition 2 result
Set up Set up Set up
Set up Failure Set up
Failure Set up Set up
Failure Failure Failure

not

not 条件
  • Non / not
condition result
Set up Failure
Failure Set up

The if statement Advanced

elif

  • In the development, use  if can be  determined conditions
  • Use  else can handle  conditions are not met  conditions
  • However, if you want  to add a number of conditions , different conditions, different code needs to be executed  when you can use elif
  • Syntax is as follows:
if 条件1:
    条件1满足执行的代码
    ……
elif 条件2:
    条件2满足时,执行的代码
    ……
elif 条件3:
    条件3满足时,执行的代码
    ……
else:
    以上条件都不满足时,执行的代码
    ……
  • Comparative logical operator code
if 条件1 and 条件2:
    条件1满足 并且 条件2满足 执行的代码
    ……

note

  1. elif And  else both must be  if used in conjunction with, but not alone
  2. It may be  if, elif and  else and each indent code, as a  complete block of code

 

if Nested

 

elif  scenario is: simultaneously  determining  a plurality of conditions , all the conditions are  same level  of

  • In the development, the use of  if conditional judgment, if you want  the conditions established in the implementation of the statement  and then  add conditions to judge , you can use  nested if the
  • if nesting  application scenario is: the premise condition is satisfied before, and then additional Analyzing
  • Nested if the  syntax format, in addition to indent  and before there is no difference
  • Syntax is as follows:
if 条件 1:
    条件 1 满足执行的代码
    ……
    
    if 条件 1 基础上的条件 2:
        条件 2 满足时,执行的代码
        ……    
        
    # 条件 2 不满足的处理
    else:
        条件 2 不满足时,执行的代码
        
# 条件 1 不满足的处理
else:
    条件1 不满足时,执行的代码
    ……

if nesting Walkthrough - Station security

  1. The definition of a Boolean variable  has_ticket indicating whether or not the ticket
  2. Definition of an integer variable  knife_length represented knife length, unit: cm
  3. First, check for tickets, if any, are allowed to carry out  security checks
  4. When security is required to check the length of the knife, it is determined whether or not more than 20 cm
    • If more than 20 cm, length of the blade tips are not allowed on the train
    • If no more than 20 cm, by screening
  5. If no ticket, do not allow the door
# 定义布尔型变量 has_ticket 表示是否有车票
has_ticket = True

# 定义整数型变量 knife_length 表示刀的长度,单位:厘米
knife_length = 20

# 首先检查是否有车票,如果有,才允许进行 安检
if has_ticket:
    print("有车票,可以开始安检...")

    # 安检时,需要检查刀的长度,判断是否超过 20 厘米
    # 如果超过 20 厘米,提示刀的长度,不允许上车
    if knife_length >= 20:
        print("不允许携带 %d 厘米长的刀上车" % knife_length)
    # 如果不超过 20 厘米,安检通过
    else:
        print("安检通过,祝您旅途愉快……")

# 如果没有车票,不允许进门
else:
    print("大哥,您要先买票啊")

Integrated application - rock-paper-scissors

 

  1. Input from the console to the boxing - a stone (1) / scissors (2) / fabric (3)
  2. Computer  random  punches - just a stone presuppose computer, complete the overall function of the code
  3. Comparison of outcome
No. rule
1 Rock wins scissors
2 Scissors wins cloth
3 Cloth win stone

1 Basic code implementation

  • First  assume that the computer will only be a stone , to complete the overall function of the code
# 从控制台输入要出的拳 —— 石头(1)/剪刀(2)/布(3)
player = int(input("请出拳 石头(1)/剪刀(2)/布(3):"))

# 电脑 随机 出拳 - 假定电脑永远出石头
computer = 1

# 比较胜负
# 如果条件判断的内容太长,可以在最外侧的条件增加一对大括号
# 再在每一个条件之间,使用回车,PyCharm 可以自动增加 8 个空格
if ((player == 1 and computer == 2) or
        (player == 2 and computer == 3) or
        (player == 3 and computer == 1)):

    print("噢耶!!!电脑弱爆了!!!")
elif player == computer:
    print("心有灵犀,再来一盘!")
else:
    print("不行,我要和你决战到天亮!")

Processing the second random number

  • In  Python , to use the random number, first you need to import  a random number  of  modules  - "toolkit"
import random
  • After import module, can be directly  module name  knocking behind a  . press  Tab button, will prompt all the functions included in the module

  • random.randint(a, b) Returns  [a, b] an integer between, comprising  a and b

  • E.g:

random.randint(12, 20)  # 生成的随机数n: 12 <= n <= 20   
random.randint(20, 20)  # 结果永远是 20   
random.randint(20, 10)  # 该语句是错误的,下限必须小于上限

Guess you like

Origin www.cnblogs.com/wkung/p/10993028.html