猫史档案馆


【Python作品分享】有了这个妈妈再也不怕我数学考0分了!0.0.2版【作业帖】

用户:嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘻嘻嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘻嘻查看:0 回复:1 评论:0 创建时间:2021-08-31T09:28:04


【作品展示】

center_image

 

【作品介绍】

优化了一下

 

【作品源代码】

#encoding: utf-8
def p_e(e_type, e_content):
    if e_type == 'o':
        #运算符错误
        return 'OperatorsError:%s%s'%('\'%s\''%(e_content),'is not defined.')
    if e_type == 'd':
        #文件名未找到
        #No files named with hhh found
        return 'DocumentsError:%s%s%s'%('No files named with ','\'%s\''%(e_content),' found.')
    if e_type == 'f':
        #功能名待开发
        #The name is invalid,the function name 'HHH' is not currently defined
        return 'FunctionNameError:%s%s%s'%('The name is invalid,the function name ','\'%s\''%(e_content),' is not currently defined')
    if e_type == "ft":
        #算式错误
        #No unexpected "hhh"
        return 'FormatError:%s%s'%('No unexpected ','\'%s\''%(e_content))

print("Welcome to version 0.0.2 of 'Mathematical assistant'.\n", 'Function:\n', "    ·'help' : Prompts you for the input format of the topic.\n", "    ·'File read' : works out the answer to a given file's math problems. currently supports addition, subtraction, multiplication, division and multiplying the 喵.\n",'    ·\'Manual entry\' : Enter the topic manually.')
print('\ninput the function name:\n')
while True:
    pi = input(">>>")
    if pi == 'help' or pi == 'Help':
        print('Numbers: Supports two integers or floating point numbers.\nOperators: currently supports \'+\' or \'-\' or \'/\' or \'*\' or \'^\', one is possible.Each question is separated by 1 space.\n')
    elif pi == 'File read' or pi == 'file read':
        fn = input('>>>    Input file name:')
        try:
            with open('%s.txt'%(str(fn)),'r') as f:
                frr = f.read()
                print('math examination questions:',frr)
        except OSError:
            print(p_e('d',fn))
        mn = ''
        mr = 0
        mr_all = []
        for i in range(len(frr)):
            #print(frr[i] == '+',mr == 0)
            if frr[i] == '=':
                if mn == '' and mr == 0:
                    print(p_e('f',frr[i]))
                    break
                if not(mn == ''):
                    try:
                        mr = eval(mn)
                    except SyntaxError:
                        print(p_e('ft','mn'))
                        break
                    except NameError:
                        print(p_e('ft','mn'))
                        break
                    mr_all.append(mr)
                    mn = ''
                    mr = 0
            elif frr[i] == ',':
                mn = 0
                mr = ''
            elif frr[i] == '^':
                mn = mn + str('**')
            else:
                mn = mn + str(frr[i])

        print('mvmvmb')
        ic = input('Select output style (write to file or print on terminal)') 
        if ic == 'write to file':
            wf_mr = ''
            wf_num = 0
            for i in frr:
                wf_mr += i
                if i == '=':
                    wf_mr += mr_all[wf_num]
                    wf_num += 1

            with open(str(input('Please input the file name.')),'w') as wfp:
                wfp.write(wf_mr)
        if ic == 'print on terminal':     
            print('solution:',mr_all)
        
    elif pi == 'Manual entry' or pi == 'manual entry':
        num = input('    Input the examination questions')
        try:
            print('solution:',eval(str(num)))
        except NameError:
            print(p_e('ft','num'))
        except SyntaxError:
            print(p_e('ft','num'))
        except TypeError:
            print(p_e('ft','num'))
    else:
        print(p_e('f',pi))

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
飞熊jSrt飞熊jSrt

真不戳(

点赞0


评论