Python第二课---核心语法
·
Pyhton入门第二课
一、核心语法
1、if语句
使用方式:if 判断条件: 条件成立 执行语句
例子:

if案例:实现登录功能
true_account = "18882636979"
true_password = "123456"
account = input("请输入您的账号:")
password= input("请输入您的密码:")
if true_account == account and true_password == password:
print("登录成功!")
else:
print("登录失败!")
if语句的其他用法:

2、模式匹配
例子:


注意:

3、循环
(1)while循环
语法:

(2)for循环
语法:

例子:

tips:
①关于range语句
其用法如下图:

②关于print
print("*",end = "")
#end表示以\n结束,即这样写不用换行
(4)嵌套循环
语法:

例子:打印一个*矩阵
a = int(input("请输入一个数字a:"))
b = int(input("请输入一个数字b:"))
for i in range(b):
for j in range(a):
print("*",end=" ")
print()
(5)循环综合案例

while True:
# 输入正确的用户名和密码
true_account = input("Enter your account: ")
true_password = input("Enter your password: ")
#校验用户名和密码是否为空
if true_account == "" or true_password == "":
print("Please enter your account and password.")
continue
#判断用户名和密码是否正确
if true_account == "admin" and true_password == "123456":
print("Welcome admin!")
break
elif true_account == "zhangsan" and true_password == "666888":
print("Welcome zhangsan!")
break
elif true_account == "dxy" and true_password == "032333":
print("Welcome dxy!")
break
else:
print("Please enter your account and password.")
更多推荐


所有评论(0)