1. 程式人生 > >Python :計算器

Python :計算器

#!/usr/bin/env python

-- coding: utf-8 --

author:albert time:2018/10/31 18:24

import math
calc = True
def panduan1(s):
try:
nb = float(s)
return True
except ValueError as e:
return False
print (’40)
print (’’‘使用說明:
1.計算器只能進行四則運算,不支援求方,求根;
2.輸入數字請不要在數字前面加0,例如:01+01;
3.輸入方法:例如:12+(12/23+3),不要外加’=‘號;
4.退出密碼:0000’’’)
while True:
kong_ji_he = []
y = [‘0’,‘0’,‘0’,‘0’]
while True:
print (’

’ * 40)
print (‘請輸入你要解答的計算公式:’)
x = input()
i = list(x)
if panduan1(i[0])== True: break
else: print(“輸入非法字元,請重新輸入:”)
if y == i:
break
ji_he = [’ ', ‘.’,‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘0’, ‘-’, ‘+’, '
’, ‘/’, ‘(’, ‘)’]
for m in i:
if m in ji_he: kong_ji_he.append(m)
else:
print(“輸入非法字元,請重新輸入:”)
break
if kong_ji_he == i:
if calc:
kong_ji_he1 =eval("".join(kong_ji_he))
print(‘你的答案是:’, kong_ji_he1)