[Python basics] day03-conditional statement (if syntax), multiple judgments, if nesting, trinocular operator, application: guessing game

the goal

  • Conditional statement effect
  • if syntax
  • if…else…
  • Multiple judgments
  • if nested

1. Understand conditional statements

Assume a scenario:

  • Have you been to internet cafes at your age?
  • What do you have to do when you want to go online to enter an Internet cafe? (Consider key points)
  • Why give the ID card to the staff?
  • Is it just to judge adulthood?
  • Is it possible to surf the Internet as an adult? If you are not an adult, you are not allowed to surf the Internet?

In fact, the so-called judgment here is a conditional statement, that is, certain codes are executed when the condition is established, and these codes are not executed if the condition is not established .

2. if syntax

2.1 Syntax

if 条件:
    条件成立执行的代码1
    条件成立执行的代码2
    ......

2.2 Quick experience

if True:
    print('条件成立执行的代码1')
    print('条件成立执行的代码2')

# 下方的代码没有缩进到if语句块,所以和if条件无关
print('我是无论条件是否成立都要执行的代码')

The execution results are as follows:
Insert picture description here

3. Example: Go online

Demand analysis: If the user's age is greater than or equal to 18 years old, that is, an adult, and the output "has been an adult and can go online".

3.1 Simple version

age = 20
if age >= 18:
    print('已经成年,可以上网')

print('系统关闭')

3.2 Advanced Edition

New requirement: Users can output their own age, and then the system will judge whether they are adults, and if they are adults, they will output "Your age is the age entered by the user, and you are already an adult and you can go online".

# input接受用户输入的数据是字符串类型,条件是age和整型18做判断,所以这里要int转换数据类型
age = int(input('请输入您的年龄:'))

if age >= 18:
    print(f'您的年龄是{age},已经成年,可以上网')


print('系统关闭')

四. if…else…

Function: the condition is met, execute the code below if; if the condition is not met, execute the code below else.

Thinking: Examples of Internet cafes are allowed to go online if they are adults. What if they are not adults? Should I reply that the user cannot access the Internet?

4.1 Syntax

if 条件:
    条件成立执行的代码1
    条件成立执行的代码2
    ......
else:
    条件不成立执行的代码1
    条件不成立执行的代码2
    ......

4.2 Practical version: Internet cafes

age = int(input('请输入您的年龄:'))

if age >= 18:
    print(f'您的年龄是{age},已经成年,可以上网')
else:
    print(f'您的年龄是{age},未成年,请自行回家写作业')

print('系统关闭')

Note: If the condition is met and some code is executed, the code in other cases will not be executed.

Five, multiple judgments

Thinking: The legal working age in China is 18-60 years old, that is, if the age is less than 18, child labor is illegal; if the age is between 18-60 years old, it is legal working age; more than 60 years old is the legal retirement age.

5.1 Syntax

if 条件1:
    条件1成立执行的代码1
    条件1成立执行的代码2
    ......
elif 条件2:
	条件2成立执行的代码1
    条件2成立执行的代码2
    ......
......
else:
    以上条件都不成立执行执行的代码

Multiple judgments can also be used in conjunction with else. Generally, the else is placed at the end of the entire if statement, indicating the code to be executed when the above conditions are not true.

5.2 Example: Judgment of service age

age = int(input('请输入您的年龄:'))
if age < 18:
    print(f'您的年龄是{age},童工一枚')
elif (age >= 18) and (age <= 60):
    print(f'您的年龄是{age},合法工龄')
elif age > 60:
    print(f'您的年龄是{age},可以退休')

Expansion: age >= 18 and age <= 60can be simplified to 18 <= age <= 60.

Six, if nesting

Thinking: Take the bus: if you have money, you can get on the bus, if you don’t have money, you can’t get on the bus; if you get on the bus, you can sit down; if you don’t have a seat, you have to stand. How to write a program?

6.1 Syntax

if 条件1:
	条件1成立执行的代码
    条件1成立执行的代码
    
    if 条件2:
    	条件2成立执行的代码
        条件2成立执行的代码
    

Note: The if of condition 2 is also in the indentation relationship of the code executed when condition 1 is established.

6.2 Example: Take the bus

6.2.1 Determine whether you can get on the bus

"""
1. 如果有钱,则可以上车
    2. 上车后,如果有空座,可以坐下
    上车后,如果没有空座,则站着等空座位
如果没钱,不能上车
"""
# 假设用 money = 1 表示有钱, money = 0表示没有钱
money = 1
if money == 1:
    print('土豪,不差钱,顺利上车')
else:
    print('没钱,不能上车,追着公交车跑')

6.2.2 Determine whether you can sit down

"""
1. 如果有钱,则可以上车
    2. 上车后,如果有空座,可以坐下
    上车后,如果没有空座,则站着等空座位
如果没钱,不能上车
"""
# 假设用 money = 1 表示有钱, money = 0表示没有钱; seat = 1 表示有空座,seat = 0 表示没有空座
money = 1
seat = 0
if money == 1:
    print('土豪,不差钱,顺利上车')
    if seat == 1:
        print('有空座,可以坐下')
    else:
        print('没有空座,站等')
else:
    print('没钱,不能上车,追着公交车跑')

7. Application: guessing game

demand analysis:

  • Characters participating in the game

    • Player
      • Manual punch
    • computer
      • Random punches
  • Win or lose

    • Player wins
    Player computer
    stone scissors
    scissors cloth
    cloth stone
    • draw
      • The player punches the same as the computer punches
    • Computer wins

Random approach:

1. 导出random模块

2. random.randint(开始,结束) # 的到从开始到结束两个数字之间的一个随机整数

The specific content and usage of the module will be explained later

"""
提示:0-石头,1-剪刀,2-布
1. 出拳
玩家输入出拳
电脑随机出拳

2. 判断输赢
玩家获胜
平局
电脑获胜
"""

# 导入random模块
import random

# 计算电脑出拳的随机数字
computer = random.randint(0, 2)
print(computer)

player = int(input('请出拳:0-石头,1-剪刀,2-布:'))

# 玩家胜利 p0:c1 或 p1:c2 或 p2:c0
if (player == 0 and computer == 1) or (player == 1 and computer == 2) or (player == 2 and computer == 0):
    print('玩家获胜')

# 平局:玩家 == 电脑
elif player == computer:
    print('平局')
else:
    print('电脑获胜')

8. Ternary operator

The ternary operator is also called the ternary operator.

The syntax is as follows:

# 条件成立执行的表达式 if 条件 else 添加不成立执行的表达式1 if 条件 else2

Quick experience:

# 取出a和b之中的最大值
a = 1
b = 2

c = a if a > b else b
print(c)

Execution result:
Insert picture description here
Example 2

'''
需求:有两个变量a,b,比较大小
    若a>b 执行 a-b ;否则执行 b-a
    
'''
a = 10
b = 6
c = a - b if a > b else b - a
print(c)

Results of the:
Insert picture description here

to sum up

  • if statement syntax
if 条件:
    条件成立执行的代码
  • if…else…
if 条件:
    条件成立执行的代码
else:
    条件不成立执行的代码
  • Multiple judgments
if 条件1:
    条件1成立执行的代码
elif 条件2:
    条件2成立执行的代码
else:
    以上条件都不成立执行的代码
  • if nested
if 条件1:
    条件1成立执行的代码
    if 条件2:
        条件2成立执行的代码
        ....

The content of this section is over, please pay attention, don’t get lost

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_38454176/article/details/112134160