菜鸟笔记
提升您的技术认知

python实现简单计算器(加减乘除)-ag真人游戏

  1. 实现加减乘除计算器
import random
count = 0
right = 0
op = [' ','-','*','/']
while true:
    a= random.randint(0,9)
    b= random.randint(0,9)
    s = random.choice(op)
    print('%d %s %d = ' %(a,s,b))
    question = input('请输入您的答案:(q退出)')
    if s == ' ':
        result = a   b
    if s == '-':
        result = a - b
    if s == '*':
        result = a * b
    if s == '/':
        result = a // b
    if question == str(result):
        print('回答正确')
        right  = 1
        count  = 1
    elif question == 'q':
        break
    else:
        print('回答错误')
        count  = 1
percent = right / count
print('测试结束,共回答%d道题,正确个数为%d,正确率为%.2f%%' %(count,right,percent * 100))
  1. 设计一个程序,帮助小学生练习10以内的加法
    题目要求:
  • 随机生成加法题目;
  • 学生查看题目并输入答案;
  • 判别学生答题是否正确?
  • 退出时, 统计学生答题总数,正确数量及正确率(保留两位小数点);
import random
s = int(input('输入题目数量:'))
ture = 0
for i in range(0,s):
    num1 = random.randint(1,9)
    num2 = random.randint(1,9)
    print('%d %d=?' %(num1,num2))
    sum = num1   num2
student = int(input('请输入答案:'))
    if sum == student :
        print('答案正确!')
        ture  = 1
    else:
        print('答案错误!')
else:
    print('ture:%d' %(ture))
    zql = (ture/s)*100
    print('学生答题总数为:%d\n正确数量为:%d\n 正确率为:%.2f%%' %(s,ture,zql))
网站地图