python---data input and if statement

 data entry and if statement

1.4 Data Input

1.4.1input

  • data can be received
  • information that can be prompted

1. Simple operation

# 读取键盘输入的数据
x=input("输入数据")
print(x)

The prompt is equivalent to

# 读取键盘输入的数据
x=input("输入数据")
print(x)

print("输入数据")
x=input()
print(x)

2. The input data is str type

  • Whatever is entered is a string

print("输入数据")
x=input()
print(x)

print(type(x))

1.4.2 input summary

summary

1.4.3 Logic _ _ _ _

1. The Boolean type is used to represent

  • Note that it is all capitalized

2. Conditional expressions are used to represent the results of Boolean type judgments

3. Definition

  • How are literals created?
  • How to get the result of a boolean calculation


#布尔类型都是大写的
print(True)
print(False)

# 判断后的
b=True
c=False
print(b)
print(c)
print(type(b))

output literal

other comparisons

# 数字比较
print(10==10)
print(5>10)
# 字符串比较
print('aa'=='cc')
print('aa'=='aa')

The comparison of inequalities, etc. are all ways that can be used for comparison

  • != Varies

1.4.4if statement

Set the conditions for judgment and the actions corresponding to the conditions

  • The basic format of the if statement is inconsistent with that of Java, etc.
  • Pay attention to the indentation and the following: it is judged according to the indentation and belongs to the if

1. Basic grammar

Complete conditional statement

2. Case

# if语句的基本格式
age =input("输入你的年龄")
age=int(age)
if age>=18:
        print("你可以访问")
        print(age)
elif age>10 and age<18:
    print("你的年龄还不算大")
    print(age)
else:
    print("你的年龄太小了")
    print(age)

summary

1.4.5 Nested use judgment statement

It is to write a multi-layer judgment statement

  • Note that the indentation you write can all rely on indentation

# 嵌套判断语句
age=input("输入您的年龄:")
age=int(age)
if age>=18:
    print("可以游玩")
elif age>=15 and age<18:
    height=input("输入您的身高:")
    if int(height)>=160:
        print("2次判断:可以游玩")
    else:
        print("2次判断,不可以")
else:
    print("不可以")

summary

Just pay attention to the indentation

1.4.6 Case

guess the number

Pay attention to the function of guessing numbers


# 猜数字
# 随机生成1-10的数字
import random
num=random.randint(1,10)
print("随机生成的数字",num)

# 要猜测的数字
guess_num=input("要猜测的数字")
guess_num=int(guess_num)

# 判断要猜测的数字
if guess_num==num:
    print("恭喜你,第一次猜测成功了。")
else:
    if guess_num>num:
        print("猜的数字大了!")
    else:
        print("猜的数字小了!")
    # 继续进行2次猜测
    guess_num = input("要猜测的数字")
    guess_num = int(guess_num)
    if guess_num == num:
        print("恭喜你,第二次猜测成功了。")
    else:
        if guess_num > num:
            print("猜的数字大了!")
        else:
            print("猜的数字小了!")
        # 继续进行3次猜测
        guess_num = input("要猜测的数字")
        guess_num = int(guess_num)
        if guess_num == num:
            print("恭喜你,第三次猜测成功了。")
        else:
            print("很遗憾,三次都失败了")

Guess the graph

Guess you like

Origin blog.csdn.net/weixin_41957626/article/details/129781748