• (双人项目)四则运算 组员:杨钰宁 闫浩楠 开发语言:Python。


    需求分析:1.适用人群:小学生。

                  2.能进行“+,—,*,/” 的四则运算。难度可以随时修改。

                  3.提交试卷后可以显示所得分数并显示错题个数。

                  4.可以显示答对的题及其打错的题的序号。

    代码如下:

    import random

    rightnum=0

    wrongnum=0

    right=[]

    wrong=[]

    for i in range(1,11):

    a=random.randint(1,9)

    b=random.randint(1,9)

    caltype=random.randint(0,3)

    if caltype == 0:

    result=a+b

    print('('+str(i)+')'+str(a)+'+'+str(b)+'=')

    inputresult=input()

    if int(inputresult) == result:

    rightnum=rightnum+1

    right.append(i)

    else:

    wrongnum=wrongnum+1

    wrong.append(i)

    if caltype == 1:

    result=a-b

    print('('+str(i)+')'+str(a)+'-'+str(b)+'=')

    inputresult=input()

    if int(inputresult) == result:

    rightnum=rightnum+1

    right.append(i)

    else:

    wrongnum=wrongnum+1

    wrong.append(i)

    if caltype == 2:

    result=a*b

    print('('+str(i)+')'+str(a)+'*'+str(b)+'=')

    inputresult=input()

    if int(inputresult) == result:

    rightnum=rightnum+1

    right.append(i)

    else:

    wrongnum=wrongnum+1

    wrong.append(i)

    if caltype == 3:

    while a%b!=0:

    a=random.randint(1,9)

    b=random.randint(1,9)

    result=round(a/b)

    print('('+str(i)+')'+str(a)+'/'+str(b)+'=')

    inputresult=input()

    if int(inputresult) == result:

    rightnum=rightnum+1

    right.append(i)

    else:

    wrongnum=wrongnum+1

    wrong.append(i)

    print('The score is:'+str(rightnum*10))

    print('wrongnumber:'+str(wrongnum))

    print('The right answer number:'+str(right))

    print('The wrong answer number:'+str(wrong))

    开发心得:本程序独立完成,疑点通过上网查询精心解决。Python里有许多已经封装好的BIF函数,可以直接进行调用。如代码中生成随机数的random.randint。这些内置函数还需多多积累。经过多次试验及网上调查后才得知Python中四则运算符调用后无法行使对应的功能,所以使用语句caltype=random.randint(0,3)随机生成0,1,2,3四个数,每个数字对应一个运算法则,实现四则运算的功能。通过两次程序的编写,对Python语言有了一定的认识。

  • 相关阅读:
    [CF-Edu113]D. Inconvenient Pairs
    第3组团队Git现场编程实战
    团队项目-选题报告
    第一次个人编程作业
    第一次软工博客作业
    (二十二)python 3 sort()与sorted()
    (二十一)python 3 内置函数
    (二十)python 3 匿名函数
    (十九)python 3 内嵌函数和闭包
    (十八)python 3 回调函数
  • 原文地址:https://www.cnblogs.com/yangyuning/p/5845869.html
Copyright © 2020-2023  润新知