计算器
#!/usr/bin/env python
#!--coding:utf-8 --
#!shenjie :2018/1/23 22:34
#!@Auther :shenjie
#!@file: 计算器.py
def add(string):
total=0
numbers=[]
numbers+=string.split('+')
for num in numbers:
total+=int(num.strip())
print('{0}={1}'.format(string,total))
def reduce(string):
result=0
numbers=[]
numbers+=string.split('-')
result=int(numbers[0].strip())
numbers.pop(0)
for num in numbers:
result-=int(num.strip())
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].strip())
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 num:(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("您输入的错误,只能输入1/2/3/4")
tuple操作
dict
其他常用操作
本文转自 iekegz 51CTO博客,原文链接:http://blog.51cto.com/jacksoner/2064426,如需转载请自行联系原作者