使用python編寫(xiě)簡(jiǎn)單計(jì)算器
?本文實(shí)例為大家分享了python編寫(xiě)簡(jiǎn)單計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
做一個(gè)計(jì)算器,這是我們想要的效果。
1、準(zhǔn)備工作
導(dǎo)入time、tqdm、math庫(kù)
from tqdm import* from time import* from math import*
2、開(kāi)始
添加一個(gè)重復(fù)循環(huán)并添加變量s
while True: ? ? #清屏 ? ? print('\033c') ? ? while True: ? ? ? ? #如果用法輸入的是str類(lèi)型將打印輸入錯(cuò)誤,再次循環(huán) ? ? ? ? try: ? ? ? ? ? ? s = int(input('''選擇一種計(jì)算方式或是退出 1、加法 2、減法 3、乘法 4、除法 5、整除 6、取余 7、乘方 8、退出 請(qǐng)輸入你的選擇:''')) ? ? ? ? ? ? break ? ? ? ? except: ? ? ? ? ? ? print('輸入錯(cuò)誤')
2.2、判斷變量s并進(jìn)行運(yùn)算
if s == 1: ? ? ? ? print('\033c') ? ? ? ? print('正在載入......') ? ? ? ? #進(jìn)度條 ? ? ? ? for i in tqdm(range(1, 500)): ? ? ? ? ? ? sleep(0.02) ? ? ? ? print('完畢!') ? ? ? ? sleep(1) ? ? ? ? print('\033c') ? ? ? ? while True: ? ? ? ? ? ? try: ? ? ? ? ? ? ? ? calculatorinput1 = int(input('請(qǐng)輸入第一個(gè)加數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請(qǐng)輸入第二個(gè)加數(shù):')) ? ? ? ? ? ? ? ? break ? ? ? ? ? ? except: ? ? ? ? ? ? ? ? print('輸入錯(cuò)誤!') ? ? ? ? #運(yùn)算 ? ? ? ? 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('請(qǐng)輸入被減數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請(qǐng)輸入減數(shù):')) ? ? ? ? ? ? ? ? break ? ? ? ? ? ? except: ? ? ? ? ? ? ? ? print('輸入錯(cuò)誤!') ? ? ? ? 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('請(qǐng)輸入第一個(gè)乘數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請(qǐng)輸入第二個(gè)乘數(shù):')) ? ? ? ? ? ? ? ? break ? ? ? ? ? ? except: ? ? ? ? ? ? ? ? print('輸入錯(cuò)誤!') ? ? ? ? c = calculatorinput1 * calculatorinput2 ? ? ? ? print(f'{calculatorinput1}乘{(lán)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('請(qǐng)輸入被除數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請(qǐng)輸入除數(shù):')) ? ? ? ? ? ? ? ? break ? ? ? ? ? ? except: ? ? ? ? ? ? ? ? print('輸入錯(cuò)誤!') ? ? ? ? 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('請(qǐng)輸入被除數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請(qǐng)輸入除數(shù):')) ? ? ? ? ? ? ? ? break ? ? ? ? ? ? except: ? ? ? ? ? ? ? ? print('輸入錯(cuò)誤!') ? ? ? ? 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('請(qǐng)輸入被除數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請(qǐng)輸入除數(shù):')) ? ? ? ? ? ? ? ? break ? ? ? ? ? ? except: ? ? ? ? ? ? ? ? print('輸入錯(cuò)誤!') ? ? ? ? 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('請(qǐng)輸入第一個(gè)數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請(qǐng)輸入第二個(gè)數(shù):')) ? ? ? ? ? ? ? ? break ? ? ? ? ? ? except: ? ? ? ? ? ? ? ? print('輸入錯(cuò)誤!') ? ? ? ? c = pow(calculatorinput1, calculatorinput2) ? ? ? ? print(f'{calculatorinput1}的{calculatorinput2}次方是{c}') ? ? ? ? sleep(3)
3、退出
使用break退出循環(huán)
elif s == 8: ? ? ? ? break
3.2、不是選項(xiàng)中的任何數(shù)字
else: ? ? ? ? print("輸入錯(cuò)誤") ? ? ? ? sleep(2)
4、全部代碼
from tqdm import* from time import* from math import* while True: ? ? #清屏 ? ? print('\033c') ? ? while True: ? ? ? ? #如果用法輸入的是str類(lèi)型將打印輸入錯(cuò)誤,再次循環(huán) ? ? ? ? try: ? ? ? ? ? ? s = int(input('''選擇一種計(jì)算方式或是退出 1、加法 2、減法 3、乘法 4、除法 5、整除 6、取余 7、乘方 8、退出 請(qǐng)輸入你的選擇:''')) ? ? ? ? ? ? break ? ? ? ? except: ? ? ? ? ? ? print('輸入錯(cuò)誤') ? ? if s == 1: ? ? ? ? print('\033c') ? ? ? ? print('正在載入......') ? ? ? ? #進(jìn)度條 ? ? ? ? for i in tqdm(range(1, 500)): ? ? ? ? ? ? sleep(0.02) ? ? ? ? print('完畢!') ? ? ? ? sleep(1) ? ? ? ? print('\033c') ? ? ? ? while True: ? ? ? ? ? ? try: ? ? ? ? ? ? ? ? calculatorinput1 = int(input('請(qǐng)輸入第一個(gè)加數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請(qǐng)輸入第二個(gè)加數(shù):')) ? ? ? ? ? ? ? ? break ? ? ? ? ? ? except: ? ? ? ? ? ? ? ? print('輸入錯(cuò)誤!') ? ? ? ? #運(yùn)算 ? ? ? ? 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('請(qǐng)輸入被減數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請(qǐng)輸入減數(shù):')) ? ? ? ? ? ? ? ? break ? ? ? ? ? ? except: ? ? ? ? ? ? ? ? print('輸入錯(cuò)誤!') ? ? ? ? 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('請(qǐng)輸入第一個(gè)乘數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請(qǐng)輸入第二個(gè)乘數(shù):')) ? ? ? ? ? ? ? ? break ? ? ? ? ? ? except: ? ? ? ? ? ? ? ? print('輸入錯(cuò)誤!') ? ? ? ? c = calculatorinput1 * calculatorinput2 ? ? ? ? print(f'{calculatorinput1}乘{(lán)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('請(qǐng)輸入被除數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請(qǐng)輸入除數(shù):')) ? ? ? ? ? ? ? ? break ? ? ? ? ? ? except: ? ? ? ? ? ? ? ? print('輸入錯(cuò)誤!') ? ? ? ? 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('請(qǐng)輸入被除數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請(qǐng)輸入除數(shù):')) ? ? ? ? ? ? ? ? break ? ? ? ? ? ? except: ? ? ? ? ? ? ? ? print('輸入錯(cuò)誤!') ? ? ? ? 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('請(qǐng)輸入被除數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請(qǐng)輸入除數(shù):')) ? ? ? ? ? ? ? ? break ? ? ? ? ? ? except: ? ? ? ? ? ? ? ? print('輸入錯(cuò)誤!') ? ? ? ? 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('請(qǐng)輸入第一個(gè)數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請(qǐng)輸入第二個(gè)數(shù):')) ? ? ? ? ? ? ? ? break ? ? ? ? ? ? except: ? ? ? ? ? ? ? ? print('輸入錯(cuò)誤!') ? ? ? ? c = pow(calculatorinput1, calculatorinput2) ? ? ? ? print(f'{calculatorinput1}的{calculatorinput2}次方是{c}') ? ? ? ? sleep(3) ? ? elif s == 8: ? ? ? ? break ? ? else: ? ? ? ? print("輸入錯(cuò)誤") ? ? ? ? sleep(2)
5、結(jié)束語(yǔ)
以上就是做一個(gè)簡(jiǎn)單計(jì)算器的過(guò)程,效果如開(kāi)頭所示。
希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python制作花瓣網(wǎng)美女圖片爬蟲(chóng)
本文通過(guò)python 來(lái)實(shí)現(xiàn)這樣一個(gè)簡(jiǎn)單的爬蟲(chóng)功能,把我們想要的圖片爬取到本地,需要的朋友可以參考下2015-10-10使用sklearn之LabelEncoder將Label標(biāo)準(zhǔn)化的方法
今天小編就為大家分享一篇使用sklearn之LabelEncoder將Label標(biāo)準(zhǔn)化的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Python實(shí)現(xiàn)自動(dòng)化處理Word文檔的方法詳解
本文主要介紹了如何使用Python實(shí)現(xiàn)Word文檔的自動(dòng)化處理,包括批量生成Word文檔、在Word文檔中批量進(jìn)行查找和替換、將Word文檔批量轉(zhuǎn)換成PDF等,希望對(duì)你有所幫助2022-08-08Python 相對(duì)路徑報(bào)錯(cuò):"No such file or 
如果你取相對(duì)路徑不是在主文件里,可能就會(huì)有相對(duì)路徑問(wèn)題:"No such file or directory",由于python 的相對(duì)路徑,相對(duì)的都是主文件所以會(huì)出現(xiàn)Python 相對(duì)路徑報(bào)錯(cuò),今天小編給大家?guī)?lái)了完美解決方案,感興趣的朋友一起看看吧2023-02-02總結(jié)Python連接CS2000的詳細(xì)步驟
今天給大家?guī)?lái)的是關(guān)于Python的相關(guān)知識(shí),文章圍繞著Python連接CS2000的詳細(xì)步驟展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06基于進(jìn)程內(nèi)通訊的python聊天室實(shí)現(xiàn)方法
這篇文章主要介紹了基于進(jìn)程內(nèi)通訊的python聊天室實(shí)現(xiàn)方法,實(shí)例分析了Python聊天室的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-06-06python3+PyQt5+Qt Designer實(shí)現(xiàn)堆疊窗口部件
這篇文章主要為大家詳細(xì)介紹了python3+PyQt5+Qt Designer實(shí)現(xiàn)堆疊窗口部件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04python?包中的sched?事件調(diào)度器的操作方法
sched模塊內(nèi)容很簡(jiǎn)單,只定義了一個(gè)類(lèi)。它用來(lái)最為一個(gè)通用的事件調(diào)度模塊,接下來(lái)通過(guò)本文給大家介紹python?包之?sched?事件調(diào)度器教程,需要的朋友可以參考下2022-04-04