Python编写一个简单计算器

简介: 一个计算器最主要的功能是加减乘除,那么用 Python 可以怎样实现呢#!/usr/bin/env python# -*- coding:utf-8 -*-# @Time : 2018/1/22 22:29# @Author : zhouyuyao# @File : daemonCalculator.

一个计算器最主要的功能是加减乘除,那么用 Python 可以怎样实现呢

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time    : 2018/1/22 22:29
# @Author  : zhouyuyao
# @File    : daemonCalculator.py
# PyCharm 2017.3.2 (Community Edition)
# Build #PC-173.4127.16, built on December 19, 2017
# JRE: 1.8.0_152-release-1024-b8 amd64
# JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
# Windows 10 10.0
# Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) 
# [MSC v.1900 64 bit (AMD64)] on win32

def add(string):
    total = 0
    numbers = []
    numbers += string.split("+")
    for num in numbers:
        total += int(num)
    print("{0}={1}".format(string,total))

def reduce(string):
    result = 0
    numbers = []
    numbers += string.split("-")
    result = int(numbers[0])
    numbers.pop(0)
    for num in numbers:
        result -= int(num)
    print("{0}={1}".format(string,result))

def ride(string):   # 乘
    total = 1
    numbers = []
    numbers += string.split("*")
    for num in numbers:
        total *= int(num.strip())
    print("{0}={1}".format(string,total))

def division(string):
    result = 0
    numbers = []
    numbers += string.split("/")
    result = int(numbers[0])
    numbers.pop(0)
    for num in numbers:
        result /= int(num.strip())
    print("{0}={1}".format(string,result))

if __name__ =="__main__":
    print("###############################")
    print("#####欢迎来到计算器工作中心######")
    print("###############################")
    print("1:加法 (a+b+c+d···)")
    print("2:减法 (a-b-c-d···)")
    print("3:乘法 (a*b*c*d···)")
    print("4:除法 (a/b/c/d···)")
    method = input("Please input number(1/2/3/4): ")
    if method == "1":
        string = input("请输入您的表达式:")
        add(string)
    elif method == "2":
        string = input("请输入您的表达式:")
        reduce(string)
    elif method == "3":
        string = input("请输入您的表达式:")
        ride(string)
    elif method == "4":
        string = input("请输入您的表达式:")
        division(string)
    else:
        print("The string you input is error.")
目录
相关文章
|
Linux Python Windows
windows python web flask 编写 Hello World
windows python web flask 编写 Hello World
windows python web flask 编写 Hello World
|
JSON 缓存 关系型数据库
linux python web flask 编写 Hello World
linux python web flask 编写 Hello World
linux python web flask 编写 Hello World
|
编译器 API C++
python 外部传参程序编写并打包exe及其调用方式
每种编程语言相互联系又相互独立,为此使用某种编程语言编写的程序都能够独立封装和生成自己的运行程序exe或者其他的API接口。而对于这样的运行程序目的往往不是用于双击使其运行的,而是通过外部传入的参数运行其中的内核函数达到某种目的的。所以在此研究python如何编写外部传参的程序,并将其封装未exe便于外部使用。
646 0
python 外部传参程序编写并打包exe及其调用方式
|
数据采集 IDE 关系型数据库
Python编程:PyThink数据库交互模块提高爬虫编写速度
Python编程:PyThink数据库交互模块提高爬虫编写速度
69 0
Python编程:PyThink数据库交互模块提高爬虫编写速度
|
Python
python编写是否是闰年
python编写是否是闰年
95 0
python编写是否是闰年
|
数据采集 存储 Shell
[oeasy]教您玩转python - 0003 - 编写 py 文件
​ [oeasy]python3-用vim编辑python文件 [点击并拖拽以移动] 编写 py 文件 🥊 回忆上次内容 上次在解释器里玩耍 了解到字符串就是给一堆字符两边加引号 可以是单引号 也可以是双引号 这样游乐场就知道 这个不是一个名字 而是一个字符串 字符串可以用print函数进行输出 但是print千万不要打错 就连大小写都不能错 我们在游乐场玩了这么久 能否写一个真正的python文件啊?🤔 编辑
183 0
 [oeasy]教您玩转python - 0003 - 编写 py 文件
|
数据采集 IDE 关系型数据库
Python编程:PyThink数据库交互模块提高爬虫编写速度
Python编程:PyThink数据库交互模块提高爬虫编写速度
116 0
|
测试技术 Python
Python编程:Flask测试用例的编写实例
Python编程:Flask测试用例的编写实例
193 0
|
Python
Python基础题型实战08-编写函数判断一张牌的花色
人生最困难的时候,也许正是转变的时候:改变固有的思想,人生就可能迎来转机。幸运,总是离努力的人更近一些。
|
存储 大数据 数据安全/隐私保护
用Python编写一个电子考勤系统
用Python编写一个电子考勤系统
245 0
用Python编写一个电子考勤系统