四则运算出题

# -*- coding: utf-8 -*-
"""
Created on Sat Sep 11 08:47:35 2021

@author: Lysim
"""

import random
from fractions import Fraction

#主干函数
def main():
    print("此软件能计算的类型有: \n1)两个整数之间加减运算  \n2)带有两位小数的两个数之间加减运算 \n3)两个分数之间加减运算")
    print("4)两个整数之间乘除法运算   \n5)两个分数之间的加减运算")
    Type=int(input("请输入要生成的题目类型代号(1,2,3,4,5,6)中的其中一个:"))
    Num=int(input("请输入要生成的题目个数:"))
    level=input("请输入你想计算的四则题目的等级(初级,中级,或高级):")
    if level=="初级":
        level=20
    elif level=="中级":
        level=50
    else:
        level=300
    count=0
    while count< Num:
        if  Type==1:
            count=count+1
            print(""+str(count)+"道题目")
            intAddSub(level)
        elif Type==2:
            count=count+1
            print(""+str(count)+"道题目")
            decAddSub(level)
        elif Type==3:
            count=count+1
            print(""+str(count)+"道题目")
            proFraAddSub(level)
        elif Type==4:
            count=count+1
            print(""+str(count)+"道题目")
            intMulDiv(level)
        elif Type==5:
            count=count+1
            print(""+str(count)+"道题目")
            proFraMulDiv(level)
        else:
            Type=input("没有该题目类型,输入错误,请重新输入:")
def intAddSub(level):
    typeR=random.randint(0,1)
    num1=random.randint(0,level)
    num2=random.randint(0,level)
    if typeR==0:
        print(str(num1)+"+"+str(num2)+"= ?")
        answer=input("请输入你的答案:")
        if(answer==str(num1+num2)):
            print("恭喜你!答对了!")
        else:
            print("很抱歉,答错了,正确答案是:",str(num1+num2))
    else:
        if num1<num2:
            mid=num1
            num1=num2
            num2= mid
        print(str(num1)+"-"+str(num2)+" = ?")
        answer=input("请输入你的答案:")
        if(answer==str(num1-num2)):
            print("恭喜你!答对了!")
        else:
            print("正确答案是:",str(num1-num2))
def decAddSub(level):
     typeR=random.randint(0,1)
     num1 = random.uniform(0, level) 
     num1=round(num1, 2)
     num2 = random.uniform(0, level) 
     num2=round(num2, 2)
     if typeR==0:
        print(str(num1)+"+"+str(num2)+"= ?")
        answer=input("请输入你的答案:")
        if(answer==format(num1+num2)):
            print("恭喜你!答对了!")
        else:
            print("正确答案是:{:.2f}".format(num1+num2))
     else:
        if num1< num2:
            mid=num1
            num1=num2
            num2= mid
        print(str(num1)+"-"+str(num2)+" = ?")
        answer=input("请输入你的答案:")
        if(answer==format(num1-num2)):
            print("恭喜你!答对了!")
        else:
            print("很遗憾,答错了,正确答案是:{:.2f}".format(num1-num2)) 
def proFraAddSub(level): 
    typeR=random.randint(0,1)
    number1=random.randint(0,level)
    number11=random.randint(1,level)
    left=Fraction(number1,number11 )
    number2=random.randint(0,level)
    number22=random.randint(1,level)
    right=Fraction(number2,number22)
    if typeR==0:
        print(str(left)+"+"+str(right)+"= ?")
        answer=input("请输入你的答案:")
        if(answer==str(Fraction(left+right))):
            print("恭喜你!答对了!")
        else:
            print("很遗憾,答错了,正确答案是:",str(Fraction(left+right)))
    else: 
        if left< right:
            middleVariable=left
            left=right
            right= middleVariable
        print(str(left)+"-"+str(right)+" = ?")
        answer=input("请输入你的答案:")
        if(answer==str(Fraction(left-right))):
            print("恭喜你!答对了!")
        else:
            print("很遗憾,答错了,正确答案是:",str(Fraction(left-right)))
def intMulDiv(level):
    typeR=random.randint(0,1) 
    if typeR==0:
        num1=random.randint(0,level)
        num2=random.randint(0,level)
        print(str(num1)+"*"+str(num2)+"= ?")
        answer=input("请输入你的答案:")
        if(answer==str(num1*num2)):
            print("恭喜你!答对了!")
        else:
            print("很遗憾,答错了,正确答案是:",str(num1*num2))
    else: 
        if level<300:
            num2=random.randint(1,level)   
            num1=num2*random.randint(0,4)  
        else:
            num1=random.randint(1,level)
            num2=random.randint(1,level)
        print(str(num1)+"÷"+str(num2)+" = ?")
        answer=input("请输入你的答案:")
        if num1%num2==0:  
            if(answer==str(int(num1/num2))):
                print("恭喜你!答对了!")
            else:
                print("很遗憾,答错了,正确答案是:",str(int(num1/num2)))
        else:
            if(answer==str(Fraction(num1,num2))):
                print("恭喜你!答对了!")
            else:
                print("很遗憾,答错了,正确答案是:",str(Fraction(num1,num2)))
def proFraMulDiv(level):
    typeR=random.randint(0,1)  
    number1=random.randint(0,level)
    number11=random.randint(1,level)
    left=Fraction(number1,number11 )
    if typeR==0:
        number2=random.randint(0,level)
        number22=random.randint(1,level)
        right=Fraction(number2,number22)
        print(str(left)+"*"+str(right)+"= ?")
        answer=input("请输入你的答案:")
        if(answer==str(left*right)):
            print("恭喜你!答对了!")
        else:
            print("正确答案是:",str(left*right))
    else:
        number2=random.randint(1,level)
        number22=random.randint(1,level)
        right=Fraction(number2,number22)
        print(str(left)+"÷"+str(right)+" = ?")
        answer==input("请输入你的答案:")
        if(answer==str(Fraction(left,right))):
            print("恭喜你!答对了!")
        else:
            print("很遗憾,答错了,正确答案是:",str(Fraction(left,right)))
main()

四则运算出题

 

四则运算出题

上一篇:多态(polymorphism)


下一篇:10个小技巧,让你的谱面更专业