使用python編寫簡單計算器
更新時間:2022年09月08日 08:44:41 作者:Xiang__jin
這篇文章主要為大家詳細介紹了使用python編寫一個簡單的計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
?本文實例為大家分享了python編寫簡單計算器的具體代碼,供大家參考,具體內容如下
做一個計算器,這是我們想要的效果。

1、準備工作
導入time、tqdm、math庫
from tqdm import* from time import* from math import*
2、開始
添加一個重復循環(huán)并添加變量s
while True:
? ? #清屏
? ? print('\033c')
? ? while True:
? ? ? ? #如果用法輸入的是str類型將打印輸入錯誤,再次循環(huán)
? ? ? ? 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退出循環(huán)
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類型將打印輸入錯誤,再次循環(huán)
? ? ? ? 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、結束語
以上就是做一個簡單計算器的過程,效果如開頭所示。
希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
使用sklearn之LabelEncoder將Label標準化的方法
今天小編就為大家分享一篇使用sklearn之LabelEncoder將Label標準化的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Python 相對路徑報錯:"No such file or 
如果你取相對路徑不是在主文件里,可能就會有相對路徑問題:"No such file or directory",由于python 的相對路徑,相對的都是主文件所以會出現Python 相對路徑報錯,今天小編給大家?guī)砹送昝澜鉀Q方案,感興趣的朋友一起看看吧2023-02-02
python3+PyQt5+Qt Designer實現堆疊窗口部件
這篇文章主要為大家詳細介紹了python3+PyQt5+Qt Designer實現堆疊窗口部件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04

