"""
1.随机生成10以内加法题目
2.学生查看题目并输入答案
3.判断学生答案是否正确
4.退出时,统计答题目总数,并统计正确数量和正确率(保留两位小数)
"""
import random
a = ['+','-','*','/']
s = random.choice(a)
print(s)
# 导入生成随机数的模块
import random
# 定义用来记录回答正确的题数和总的题数
right = 0
counts = 0
# 无限循环
while True:
# 生成0-10的随机数
a = random.randint(0,10)
b = random.randint(0,10)
# 输出题目
print('%d + %d =' %(a,b))
# 接收用户输入的答案
question = input('请输入您的答案(q退出): ')
# 计算正确答案
result = a + b
# 判断答案是否正确
if question == str(result):
print('回答正确')
right += 1
counts += 1
# break表示退出当前循环
elif question == 'q':
break
else:
print('回答错误')
# 回答正确的题数与回答错误的题数之和便是总的答题数目
counts += 1
# 计算正确率(除数不能为0)
if counts == 0:
percent = 0
else:
percent = right / counts
# 两个%%表示一个%
print ('测试结束,共回答%d道题,正确个数为%d,正确率为:%.2f%%' %(counts,right,percent*100))
"""
1.随机生成1~100的数字
2.5次机会
3.too big
4.too small
5.恭喜,并退出循环
"""
import random ##随机选出一个数
trycount = 0
computer = random.randint(1,100) ##在1~100之间
print(computer)
while trycount < 5: ##控制最大循环次数为5次
player = int(input('Num: '))
if player > computer:
print('too big')
trycount += 1 ##控制循环
elif player < computer:
print('too small')
trycount += 1
else:
print('恭喜')
break
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d4df4ca7d351a0a12e2d297e6139f99b.png)
#for i in range(3):
trycount = 0
while trycount < 3: ##用while语句实现三次循环
name = input('用户名: ')
passwd = input('密码: ')
if name == 'root' and passwd == 'westos':
print('登录成功!')
break
else:
print('登录失败')
print('您还剩余%d次机会' %(2-trycount))
trycount += 1
else:
print('失败超过3次,请稍后再试!')
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6cd76a251473395a374a31bfdd1fc9d4.png)
输出给定图案
*
**
***
****
*****
*****
****
***
**
*
*
**
***
****
*****
*****
****
***
**
*
"""
row = 1
while row <= 5: ##行数
col = 1 ##列数
while col <= row: ##while循环实现递加
print('*',end='')
col += 1
print('') ##空行
row += 1
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/894d0c30abe9c1bcdd8cc5a989bcb7b9.png)
"""
输出九九乘法表
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
"""
row = 1
while row <= 9:
col = 1
while col <= row:
print('%d * %d = %d\t' %(row,col,col*row),end='')
col += 1
print('')
row += 1
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d06960242a96cf30007695df5e33db76.png)