猫史档案馆


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

用户:嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘻嘻嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘻嘻查看:2 回复:1 评论:2 创建时间:2021-08-30T20:23:36


【作品展示】

center_image

 

【作品介绍】

考虑到通用和普遍性,所以使用了英文,目前有一些功能未开发。

文本文件输入格式示例:

1+1=,2+1=

使用英文逗号

 

【作品源代码】

#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.1 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('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下一页
伴雪纷飞伴雪纷飞

一行代码解决

while True:print(eval(input('>>>')))

点赞0


评论