使用python編寫簡(jiǎn)單計(jì)算器
?本文實(shí)例為大家分享了python編寫簡(jiǎn)單計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
做一個(gè)計(jì)算器,這是我們想要的效果。

1、準(zhǔn)備工作
導(dǎo)入time、tqdm、math庫
from tqdm import* from time import* from math import*
2、開始
添加一個(gè)重復(fù)循環(huán)并添加變量s
while True:
? ? #清屏
? ? print('\033c')
? ? while True:
? ? ? ? #如果用法輸入的是str類型將打印輸入錯(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類型將打印輸入錯(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é)束語
以上就是做一個(gè)簡(jiǎn)單計(jì)算器的過程,效果如開頭所示。
希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python簡(jiǎn)單實(shí)現(xiàn)獲取當(dāng)前時(shí)間
最近項(xiàng)目中經(jīng)常需要python去取當(dāng)前的時(shí)間,雖然不是很難,但是老是忘記,用一次丟一次,為了能夠更好的記住,我今天特意寫下python 當(dāng)前時(shí)間這篇文章,如果你覺的對(duì)你有用的話,可以收藏下。2016-08-08
python實(shí)現(xiàn)時(shí)間o(1)的最小棧的實(shí)例代碼
這篇文章主要介紹了python實(shí)現(xiàn)時(shí)間o(1)的最小棧的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
Python實(shí)現(xiàn)迷宮自動(dòng)尋路實(shí)例
大家好,本篇文章主要講的是Python實(shí)現(xiàn)迷宮自動(dòng)尋路實(shí)例,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02
Selenium基于PIL實(shí)現(xiàn)拼接滾動(dòng)截圖
這篇文章主要介紹了Selenium基于PIL實(shí)現(xiàn)拼接滾動(dòng)截圖,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Python?數(shù)據(jù)分析教程探索性數(shù)據(jù)分析
這篇文章主要介紹了Python?數(shù)據(jù)分析教程探索性數(shù)據(jù)分析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
pytorch 實(shí)現(xiàn)cross entropy損失函數(shù)計(jì)算方式
今天小編就為大家分享一篇pytorch 實(shí)現(xiàn)cross entropy損失函數(shù)計(jì)算方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python 安裝 virturalenv 虛擬環(huán)境的教程詳解
這篇文章主要介紹了Python 安裝 virturalenv 虛擬環(huán)境的教程,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
解決linux下使用python打開terminal時(shí)報(bào)錯(cuò)的問題
這篇文章主要介紹了linux下使用python打開terminal時(shí)報(bào)錯(cuò),本文通過兩種場(chǎng)景分析給大家詳細(xì)講解,需要的朋友可以參考下2023-03-03

