python编写简单计算器
Xiang__jin 人气:0
做一个计算器,这是我们想要的效果。
1、准备工作
导入time、tqdm、math库
from tqdm import* from time import* from math import*
2、开始
添加一个重复循环并添加变量s
while True: #清屏 print('\033c') while True: #如果用法输入的是str类型将打印输入错误,再次循环 try: s = int(input('''选择一种计算方式或是退出 1、加法 2、减法 3、乘法 4、除法 5、整除 6、取余 7、乘方 8、退出 请输入你的选择:''')) break except: print('输入错误')
2.2、判断变量s并进行运算
if s == 1: print('\033c') print('正在载入......') #进度条 for i in tqdm(range(1, 500)): sleep(0.02) print('完毕!') sleep(1) print('\033c') while True: try: calculatorinput1 = int(input('请输入第一个加数:')) calculatorinput2 = int(input('请输入第二个加数:')) break except: print('输入错误!') #运算 c = calculatorinput1 + calculatorinput2 print(f'{calculatorinput1}加{calculatorinput2}等于{c}') sleep(3)
2.3、依次添加减法、乘法、除法、整除、取余、乘方
elif s == 2: print('\033c') print('正在载入......') for i in tqdm(range(1, 500)): sleep(0.02) print('完毕!') sleep(1) print('\033c') while True: try: calculatorinput1 = int(input('请输入被减数:')) calculatorinput2 = int(input('请输入减数:')) break except: print('输入错误!') c = calculatorinput1 - calculatorinput2 print(f'{calculatorinput1}减{calculatorinput2}等于{c}') sleep(3) elif s == 3: print('\033c') print('正在载入......') for i in tqdm(range(1, 500)): sleep(0.02) print('完毕!') sleep(1) print('\033c') while True: try: calculatorinput1 = int(input('请输入第一个乘数:')) calculatorinput2 = int(input('请输入第二个乘数:')) break except: print('输入错误!') c = calculatorinput1 * calculatorinput2 print(f'{calculatorinput1}乘{calculatorinput2}等于{c}') sleep(3) elif s == 4: print('\033c') print('正在载入......') for i in tqdm(range(1, 500)): sleep(0.02) print('完毕!') sleep(1) print('\033c') while True: try: calculatorinput1 = int(input('请输入被除数:')) calculatorinput2 = int(input('请输入除数:')) break except: print('输入错误!') c = calculatorinput1 / calculatorinput2 print(f'{calculatorinput1}除以{calculatorinput2}等于{c}') sleep(3) elif s == 5: print('\033c') print('正在载入......') for i in tqdm(range(1, 500)): sleep(0.02) print('完毕!') sleep(1) print('\033c') while True: try: calculatorinput1 = int(input('请输入被除数:')) calculatorinput2 = int(input('请输入除数:')) break except: print('输入错误!') c = calculatorinput1 // calculatorinput2 print(f'{calculatorinput1}整除{calculatorinput2}等于{c}') sleep(3) elif s == 6: print('\033c') print('正在载入......') for i in tqdm(range(1, 500)): sleep(0.02) print('完毕!') sleep(1) print('\033c') while True: try: calculatorinput1 = int(input('请输入被除数:')) calculatorinput2 = int(input('请输入除数:')) break except: print('输入错误!') c = calculatorinput1 % calculatorinput2 print(f'{calculatorinput1}取余{calculatorinput2}等于{c}') sleep(3) elif s == 7: print('\033c') print('正在载入......') for i in tqdm(range(1, 500)): sleep(0.02) print('完毕!') sleep(1) print('\033c') while True: try: calculatorinput1 = int(input('请输入第一个数:')) calculatorinput2 = int(input('请输入第二个数:')) break except: print('输入错误!') c = pow(calculatorinput1, calculatorinput2) print(f'{calculatorinput1}的{calculatorinput2}次方是{c}') sleep(3)
3、退出
使用break退出循环
elif s == 8: break
3.2、不是选项中的任何数字
else: print("输入错误") sleep(2)
4、全部代码
from tqdm import* from time import* from math import* while True: #清屏 print('\033c') while True: #如果用法输入的是str类型将打印输入错误,再次循环 try: s = int(input('''选择一种计算方式或是退出 1、加法 2、减法 3、乘法 4、除法 5、整除 6、取余 7、乘方 8、退出 请输入你的选择:''')) break except: print('输入错误') if s == 1: print('\033c') print('正在载入......') #进度条 for i in tqdm(range(1, 500)): sleep(0.02) print('完毕!') sleep(1) print('\033c') while True: try: calculatorinput1 = int(input('请输入第一个加数:')) calculatorinput2 = int(input('请输入第二个加数:')) break except: print('输入错误!') #运算 c = calculatorinput1 + calculatorinput2 print(f'{calculatorinput1}加{calculatorinput2}等于{c}') sleep(3) elif s == 2: print('\033c') print('正在载入......') for i in tqdm(range(1, 500)): sleep(0.02) print('完毕!') sleep(1) print('\033c') while True: try: calculatorinput1 = int(input('请输入被减数:')) calculatorinput2 = int(input('请输入减数:')) break except: print('输入错误!') c = calculatorinput1 - calculatorinput2 print(f'{calculatorinput1}减{calculatorinput2}等于{c}') sleep(3) elif s == 3: print('\033c') print('正在载入......') for i in tqdm(range(1, 500)): sleep(0.02) print('完毕!') sleep(1) print('\033c') while True: try: calculatorinput1 = int(input('请输入第一个乘数:')) calculatorinput2 = int(input('请输入第二个乘数:')) break except: print('输入错误!') c = calculatorinput1 * calculatorinput2 print(f'{calculatorinput1}乘{calculatorinput2}等于{c}') sleep(3) elif s == 4: print('\033c') print('正在载入......') for i in tqdm(range(1, 500)): sleep(0.02) print('完毕!') sleep(1) print('\033c') while True: try: calculatorinput1 = int(input('请输入被除数:')) calculatorinput2 = int(input('请输入除数:')) break except: print('输入错误!') c = calculatorinput1 / calculatorinput2 print(f'{calculatorinput1}除以{calculatorinput2}等于{c}') sleep(3) elif s == 5: print('\033c') print('正在载入......') for i in tqdm(range(1, 500)): sleep(0.02) print('完毕!') sleep(1) print('\033c') while True: try: calculatorinput1 = int(input('请输入被除数:')) calculatorinput2 = int(input('请输入除数:')) break except: print('输入错误!') c = calculatorinput1 // calculatorinput2 print(f'{calculatorinput1}整除{calculatorinput2}等于{c}') sleep(3) elif s == 6: print('\033c') print('正在载入......') for i in tqdm(range(1, 500)): sleep(0.02) print('完毕!') sleep(1) print('\033c') while True: try: calculatorinput1 = int(input('请输入被除数:')) calculatorinput2 = int(input('请输入除数:')) break except: print('输入错误!') c = calculatorinput1 % calculatorinput2 print(f'{calculatorinput1}取余{calculatorinput2}等于{c}') sleep(3) elif s == 7: print('\033c') print('正在载入......') for i in tqdm(range(1, 500)): sleep(0.02) print('完毕!') sleep(1) print('\033c') while True: try: calculatorinput1 = int(input('请输入第一个数:')) calculatorinput2 = int(input('请输入第二个数:')) break except: print('输入错误!') c = pow(calculatorinput1, calculatorinput2) print(f'{calculatorinput1}的{calculatorinput2}次方是{c}') sleep(3) elif s == 8: break else: print("输入错误") sleep(2)
5、结束语
以上就是做一个简单计算器的过程,效果如开头所示。
希望对大家的学习有所帮助,也希望大家多多支持。
加载全部内容