python生日计算器_[ Python ] 计算器

#-*- coding: utf-8 -*-#Author: hkey

importredefcheck_exp(get_input):'''检查表达式中的字符是否存在不合法性,合法返回真,不合法返回假

:param get_input: 用户输入的表达式,待检测的表达式

:return: 返回真或者假'''char_set= {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','+', '-', '*', '/', '(', ')', '**', '%', '//', '.', } #有效表达式的字符集合

#判断用户输入的表达式字符是否是char_set的子集,如果是则为真,否则为假

if set(get_input) <=char_set:#如果表达式以 '*, /, %, )'开头,则为假

if get_input.startswith('*') or get_input.startswith('/') or get_input.startswith('%') \or get_input.startswith(')'):print('\33[31;1m输入的表达式有误,请重新输入\33[0m')returnFalse#如果表达式以 '+, -, *, /, ('结尾,则为假

elif get_input.endswith('+') or get_input.endswith('-') or get_input.endswith('*') \or get_input.endswith('/') or get_input.endswith('('):print('\33[31;1m输入的表达式有误,请重新输入\33[0m')returnFalse#如果表达式中含有 '.., +/, +/, -*, -/, -%',则为假

elif '..' in get_input or '+*' in get_input or '+/' in get_input or '+%' inget_input \or '-*' in get_input or '-/' in get_input or '-%' in get_input or '%%' inget_input:print('\33[31;1m输入的表达式有误,请重新输入\33[0m')returnFalseelse:returnTrueelse:print('\33[31;1m表达式中包含其他字符,无法进行计算,请重新输入\33[0m')returnFalsedefsimple_exp(exp):'''简化表达式,如'+-'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值