第三讲
运算符
# + - * / % 加减乘除求余
number = input('请输入数字')
number = int(number) # 输入的是字符类型,用int转换为整数类型进行运算
result = number % 2
print(result)
# //表示整除运算
bounty = 5000
print(bounty/280) # 结果是17.86
print(bounty//280) # 结果是17
# += *= /= -= 运算符
# 关系运算符返回值类型是布尔类型
# ord()函数可以查看字符对应的整数数值
print(ord('a'))
# 逻辑运算符 and or not
# and 有假就为假 or 有真就为真 not 真就假假就真
第四讲
条件语句
- if语句的使用
- if…else语句的使用
- 多重if语句的使用
- if嵌套
# if 要判断的条件: (标准结构)
# 条件成立时要做的事情
# else:
# 条件成立时要做的事情
money = input('请输入您的钱数')
money = int(money) # 注意转换
things1 = '盲盒'
if money>35:
print('拿下{}'.format(things1))
# 用{}进行占位,用.format()函数填充占位
else:
print('买不起')
# elif语句
# if 条件1:
# 事情1
# elif 条件2:
# 事情2
# else:
# 事情3
choice1 = input('请输入你要选的品牌')
choice2 = input('请输入你要选的衣服类型')
money = input('请输入你手中有多少钱')
money = int(money)
if choice1 == '安踏' and choice2 == '短袖' and money > 60 :
print('您购买了{}品牌的{}'.format(choice1,choice2), '您还有{}元钱'.format(money-60) , sep = '\n')
elif choice1 == '李宁' and choice2 == '短袖' and money > 60 :
print('您购买了{}品牌的{}'.format(choice1,choice2), '您还有{}元钱'.format(money-60) , sep = '\n')
elif choice1 == '耐克' and choice2 == '短袖' and money > 60:
print('您购买了{}品牌的{}'.format(choice1, choice2), '您还有{}元钱'.format(money - 60), sep='\n')
else:
print('您要的我们没有或者您的钱不足以支付请重试')
# if下语句可继续嵌套if
第五讲
循环语句
- while的使用
- for的使用
- break和continue的使用
- while或者for与else结合使用
循环的核心在于减少重复代码,条件要尽量通用化
# while 循环条件:(当循环条件不成立时结束)
# 循环执行的代码
i = input('请输入数字')
i = int(i)
while i <= 5:
i += 1
print(i)
# 需求:限制登陆次数
i = 3
while i >= 1:
username = input('请输入用户名')
password = input('请输入密码')
if username == 'admin' and password == '123456' :
print('登录成功')
break # 作用在于登录成功后退出循环
else:
print('登录失败')
i -= 1
print('还有{}次机会' .format(i))
continue # 继续循环
# for 变量 in 序列:
# 循环体
# 变量:自定义变量名,要求和单独定义时候一样
# 序列:容器型数据类型的数据,比如字符串,布尔,列表,元组,集合
# 循环体:需要重复执行的代码
for x in range(1,11):
print('今天你直播下单{}次'.format(x))
# range()函数作用是产生一个序列,从0开始
# range(1,11)则表示从1开始11结束但是不包括11
# range(1,6,2)表示135的序列,即2为步长(可以是负数)
# 需求:限制登陆次数
for i in range(3):
username = input('请输入用户名')
password = input('请输入密码')
if username == 'admin' and password == '123456' :
print('登录成功')
break # 作用在于登录成功后退出循环
else:
print('登录失败')
print('还有{}次机会' .format(2-i))
continue # 继续循环
# continue 跳过本次循环,后面语句不执行,继续执行下一次循环
# 需求:打印1-12数字,除了8不打印
# 方法一
for i in range(1,13):
if i == 8:
continue
else:
print(i)
# 方法二
i = 1
while i <12:
i += 1
if i == 8 :
continue
print(i)
# else可以和while,for进行配合
# 案例一
i = 13
while i <12:
print('验证失败')
else:
print('验证成功')
# 案例二
for i in range(8):
print(i , end=' ')
else:
print('\n')
print('结束')