使用python編寫簡單計算器
?本文實例為大家分享了python編寫簡單計算器的具體代碼,供大家參考,具體內(nèi)容如下
做一個計算器,這是我們想要的效果。
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('請輸入第一個加數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請輸入第二個加數(shù):')) ? ? ? ? ? ? ? ? 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('請輸入被減數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請輸入減數(shù):')) ? ? ? ? ? ? ? ? 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('請輸入第一個乘數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請輸入第二個乘數(shù):')) ? ? ? ? ? ? ? ? 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('請輸入被除數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請輸入除數(shù):')) ? ? ? ? ? ? ? ? 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('請輸入被除數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請輸入除數(shù):')) ? ? ? ? ? ? ? ? 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('請輸入被除數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請輸入除數(shù):')) ? ? ? ? ? ? ? ? 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('請輸入第一個數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請輸入第二個數(shù):')) ? ? ? ? ? ? ? ? break ? ? ? ? ? ? except: ? ? ? ? ? ? ? ? print('輸入錯誤!') ? ? ? ? c = pow(calculatorinput1, calculatorinput2) ? ? ? ? print(f'{calculatorinput1}的{calculatorinput2}次方是{c}') ? ? ? ? sleep(3)
3、退出
使用break退出循環(huán)
elif s == 8: ? ? ? ? break
3.2、不是選項中的任何數(shù)字
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('請輸入第一個加數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請輸入第二個加數(shù):')) ? ? ? ? ? ? ? ? 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('請輸入被減數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請輸入減數(shù):')) ? ? ? ? ? ? ? ? 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('請輸入第一個乘數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請輸入第二個乘數(shù):')) ? ? ? ? ? ? ? ? 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('請輸入被除數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請輸入除數(shù):')) ? ? ? ? ? ? ? ? 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('請輸入被除數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請輸入除數(shù):')) ? ? ? ? ? ? ? ? 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('請輸入被除數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請輸入除數(shù):')) ? ? ? ? ? ? ? ? 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('請輸入第一個數(shù):')) ? ? ? ? ? ? ? ? calculatorinput2 = int(input('請輸入第二個數(shù):')) ? ? ? ? ? ? ? ? break ? ? ? ? ? ? except: ? ? ? ? ? ? ? ? print('輸入錯誤!') ? ? ? ? c = pow(calculatorinput1, calculatorinput2) ? ? ? ? print(f'{calculatorinput1}的{calculatorinput2}次方是{c}') ? ? ? ? sleep(3) ? ? elif s == 8: ? ? ? ? break ? ? else: ? ? ? ? print("輸入錯誤") ? ? ? ? sleep(2)
5、結(jié)束語
以上就是做一個簡單計算器的過程,效果如開頭所示。
希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用sklearn之LabelEncoder將Label標準化的方法
今天小編就為大家分享一篇使用sklearn之LabelEncoder將Label標準化的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Python實現(xiàn)自動化處理Word文檔的方法詳解
本文主要介紹了如何使用Python實現(xiàn)Word文檔的自動化處理,包括批量生成Word文檔、在Word文檔中批量進行查找和替換、將Word文檔批量轉(zhuǎn)換成PDF等,希望對你有所幫助2022-08-08Python 相對路徑報錯:"No such file or 
如果你取相對路徑不是在主文件里,可能就會有相對路徑問題:"No such file or directory",由于python 的相對路徑,相對的都是主文件所以會出現(xiàn)Python 相對路徑報錯,今天小編給大家?guī)砹送昝澜鉀Q方案,感興趣的朋友一起看看吧2023-02-02基于進程內(nèi)通訊的python聊天室實現(xiàn)方法
這篇文章主要介紹了基于進程內(nèi)通訊的python聊天室實現(xiàn)方法,實例分析了Python聊天室的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2015-06-06python3+PyQt5+Qt Designer實現(xiàn)堆疊窗口部件
這篇文章主要為大家詳細介紹了python3+PyQt5+Qt Designer實現(xiàn)堆疊窗口部件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04python?包中的sched?事件調(diào)度器的操作方法
sched模塊內(nèi)容很簡單,只定義了一個類。它用來最為一個通用的事件調(diào)度模塊,接下來通過本文給大家介紹python?包之?sched?事件調(diào)度器教程,需要的朋友可以參考下2022-04-04