当前位置:   article > 正文

Python之--while练习_python while语句练习

python while语句练习
"""
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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

"""
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

在这里插入图片描述

#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次,请稍后再试!')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在这里插入图片描述

输出给定图案    

*
**
***
****
*****

*****
****
***
**
*

    *
   **
  ***
 ****
*****

*****
 ****
  ***
   **
    *
"""
row = 1

while row <= 5:         ##行数
    col = 1		##列数
    while col <= row:	##while循环实现递加
        print('*',end='')
        col += 1
    print('')		##空行
    row += 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

在这里插入图片描述

"""
输出九九乘法表
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/54852?site
推荐阅读
相关标签
  

闽ICP备14008679号