@
猜拳游戏复习if语句
猜拳游戏
a. 两个角色 玩家 player - 电脑 robot
b. 动作: 石头 0 , 剪刀 1, 布 2
c. 我的出拳: 由输入完成
d. 电脑的出拳: 随机数完成
e. 比较出拳
f. 相等 - 平局
g. 玩家赢: p0:r1 p1:r2 p2:r0
h. 剩下的情况就是电脑赢
# 导入随机数模块
import random
# 定义一个函数
def game():
# 定义一个玩家变量,从键盘输入值
player = int(input('请输入一个状态(石头 0 , 剪刀 1, 布 2):'))
# 定义一个电脑变量,使用随机数获取状态
robot = random.randint(0, 2)
# 比较状态
# 先状态平局
if player == robot:
print('平局')
# 再判断玩家赢的状态
elif ((player == 0) and (robot == 1)) or ((player == 1) and (robot == 2)) or ((player == 2) and (robot == 0)):
print('玩家赢')
# 剩下就是电脑赢的状态
else:
print('你输出,电脑胜出')
# 函数调用,开始游戏
game()
一、if语句实现的三目运算符
c语言: 条件? 值1 : 值2
python实现:
值1 if 条件 else 值2
执行过程:
判断 条件是否成立,如果条件成立,那么得到值1, 否则得到值2
def test_func():
# 输入一个数,判断奇偶
n = int(input('number:'))
# s = '偶数' if n % 2 == 0 else '奇数'
s = '奇数' if n % 2 != 0 else '偶数'
print(s)
test_func()
二、while循环语句
程序分为三种执行结构 :
a. 顺序结构 -> 从上至小,顺序执行
b. 分支结构 -> 根据条件是否成立,选择不同的代码执行
c. 循环结构 -> 一段代码会重复执行多次
循环四要素:
a. 循环变量初始值
b. 循环条件
c. 循环体代码
d. 让循环变量趋近于结束条件的代码
循环执行过程:
以上方描述的 abcd 做为过程
a -> b -> c -> d -> b -> c -> d -> b -> c -> d ->… b(不满足条件,退出循环)
while 格式
while 条件:
循环体代码
循环实现1-100的输出
```python
def test_func():
# 定义一个循环变量并有初始值
i = 1
# 判断条件
while i <= 100:
# 循环体
print(i)
# 改变循环变量
i += 1
# 调用函数执行
test_func()
循环的练习–输出矩形
def print_rect():
i = 0
while i < 5:
j = 0
while j < 5:
# 在print函数中,使用end= 形式来给print函数设置一个结束符号,默认的结束符是\n换行
print('*',end=' ')
j += 1
print()
i += 1
print_rect()
四、while循环嵌套
while 条件1:
while 条件2:
循环体…
执行过程:
外层循环循环一次,内层循环循环一圈
i = 0
while i < 3:
print('i -- ',i)
j = 0
while j < 5:
print('j -- ',j)
j += 1
i += 1
练习:计算1-100累计加
1-100的累加和,即1 + 2 + 3 + 4 +….,即前两个数字的相加结果 + 下⼀个数字( 前⼀个数字 +1)
i = 1
result = 0
while i <= 100:
result += i
i += 1
print(result)
打印星号
# 重复打印5行星星
j = 0
while j <= 4:
# 一行星星的打印
i = 0
while i <= 4:
# 一行内的星星不能换行,取消print默认的结束符\n
print('*',end ='')
i += 1
# 每行结束后进行换行,借助print,利用print默认换行
print()
j += 1
打印星号(三角形)
# 重复打印5行星星
# j代表行号
j = 0
while j <= 4:
# 一行星星的打印
i = 0
# i代表每行的星星数,需要与j进行联动
while i <= j:
print('*',end='')
i += 1
print()
j += 1
九九乘法表打印
# 重复打印九行表达式
j = 0
while j <= 9:
# 打印一行的表达式
i= 1
while i <= j:
print(f'{i}*{j}={i*j}',end='\t')
i += 1
print()
j += 1
三、break和continue
break
1.break 只能用在循环里
2.break 的作用是用来结束循环,不管循环还有多少次
ef test_func():
i = 1
print('enter loop')
while i <= 100:
print(i)
if i % 2 == 0:
break
i += 1
# break
print('exit loop')
# break #SyntaxError: 'break' outside loop
test_func()
continue
1.continue 也只能用在循环里
2. continue 的作用是用来结束本次循环,不管循环体中还有多少代码没有执行,进入下一次循环
def test_func():
i = 1
while i <= 5:
i += 1
print('hello')
continue
print('world')
test_func()
四、for循环
** 语法**
for 临时变量 in 序列:
重复执行的代码1
重复执行的代码2
def test_func1():
# 得到字符串中的所有字符
for c in 'abcdefg':
# 将小写字母变成大写输出
print(c.upper())
# for-in 循环如果需要计数,需要配合 range() 实现
# range 有两个参数,参数一是起始值 ,参数二是终止值
# 得到一个数字区间,是一个左闭右开区间, [start, end)
# 如果只给一个参数,那么默认是终止值 ,起始值默认是 0
def test_func2():
for i in range(10, 20):
print(i)
def test_func3():
# 如果需 要计数,但又不需 要用到这个数字时,可以没有循环变量
for _ in range(5):
print('hello', _)
# test_func1()
# test_func2()
test_func3()
五、else
循环可以配合else使用,else下方缩进的代码指的是当循环结束之后要执行的代码
语法
while 条件:
条件成立重复执行代码;
else:
循环正常结束后要执行的代码
循环后面有else的场景
def test_func1(s):
for c in s:
print(c)
if c == 'A':
print('字符串里有A')
break
else:
print('没有A')
def test_func2():
i = 0
while i < 10:
print(i)
if i == 50:
print('中断结束循环')
break
i += 1
else:
print('程序没有被中断,正常结束')
test_func1('helAlo')
# test_func2()
while …else
需求:⼥朋友⽣⽓了,要惩罚:连续说5遍“我错了”,如果道歉正常完毕⼥朋友就原谅我了,这
个程序怎么写?
while
i = 1
while i <= 5:
print('我错了')
i += 1
print('原谅我了')
while…else
i = 1
while i <= 5:
print('我错了')
i += 1
else:
print('原谅我了,真开心')
退出循环
break
在程序执行过程中,只要满足了break的条件,执行了break就会跳出循环,不再执行break后面的内容
i = 1
while i <= 5:
if i== 3:
print('这遍说的不真,不用说了')
break
print('我错了')
i += 1
else:
print('原谅我了,真开心')
continue
continue是退出当前⼀次循环,继续下⼀次循环,所以该循环在continue控制下是可以正常
结束的,当循环结束后,则执⾏了else缩进的代码
i = 1
while i <= 5:
if i== 3:
print('这遍说的不真')
i += 1
continue
print('我错了')
i += 1
else:
print('原谅我了,真开心')
for …else
for 临时变量 in 序列:
重复执⾏的代码
…
else:
循环正常结束之后要执⾏的代码
str = 'helloahhaha'
for i in str:
print(i)
else:
print('循环正常结束后执行的代码')
break 终止循环
str = 'helloahhaha'
for i in str:
if i == 'e':
print('遇到e不打印')
break
print(i)
else:
print('循环正常结束后执行的代码')
执行结果
continue
str = 'helloahhaha'
for i in str:
if i == 'e':
print('遇到e不打印')
continue
print(i)
else:
print('循环正常结束后执行的代码')
执行结果
自定义实现int函数,将传入函数的字符串转换成整型变量,可实现乘法
def my_int(s):
# 定义一个用来返回转换后的变量
return_num = 0
# 遍历字符串,拿到字符串的每一个字符
for c in s :
# 利用拿到的字符,通过ord函数,和字符0的ASCII码进行转换,得到差值
n = ord(c) - ord('0')
# 进行数据的累加
return_num = return_num * 10 + n
return return_num
m = my_int('234')
print(my_int('234'))
print(m * 10)
运行结果