欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用python編寫簡單計算器

 更新時間:2022年09月08日 08:44:41   作者:Xiang__jin  
這篇文章主要為大家詳細介紹了使用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)文章

  • python制作花瓣網(wǎng)美女圖片爬蟲

    python制作花瓣網(wǎng)美女圖片爬蟲

    本文通過python 來實現(xiàn)這樣一個簡單的爬蟲功能,把我們想要的圖片爬取到本地,需要的朋友可以參考下
    2015-10-10
  • Python hashlib加密模塊常用方法解析

    Python hashlib加密模塊常用方法解析

    這篇文章主要介紹了Python hashlib加密模塊常用方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • 使用sklearn之LabelEncoder將Label標準化的方法

    使用sklearn之LabelEncoder將Label標準化的方法

    今天小編就為大家分享一篇使用sklearn之LabelEncoder將Label標準化的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Python實現(xiàn)自動化處理Word文檔的方法詳解

    Python實現(xiàn)自動化處理Word文檔的方法詳解

    本文主要介紹了如何使用Python實現(xiàn)Word文檔的自動化處理,包括批量生成Word文檔、在Word文檔中批量進行查找和替換、將Word文檔批量轉(zhuǎn)換成PDF等,希望對你有所幫助
    2022-08-08
  • Python 相對路徑報錯:"No such file or directory"'原因及解決方法

    Python 相對路徑報錯:"No such file or 

    如果你取相對路徑不是在主文件里,可能就會有相對路徑問題:"No such file or directory",由于python 的相對路徑,相對的都是主文件所以會出現(xiàn)Python 相對路徑報錯,今天小編給大家?guī)砹送昝澜鉀Q方案,感興趣的朋友一起看看吧
    2023-02-02
  • Pygame中Sprite的使用方法示例詳解

    Pygame中Sprite的使用方法示例詳解

    這篇文章主要介紹了Pygame中Sprite的使用方法,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-09-09
  • 總結(jié)Python連接CS2000的詳細步驟

    總結(jié)Python連接CS2000的詳細步驟

    今天給大家?guī)淼氖顷P(guān)于Python的相關(guān)知識,文章圍繞著Python連接CS2000的詳細步驟展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 基于進程內(nèi)通訊的python聊天室實現(xiàn)方法

    基于進程內(nèi)通訊的python聊天室實現(xiàn)方法

    這篇文章主要介紹了基于進程內(nèi)通訊的python聊天室實現(xiàn)方法,實例分析了Python聊天室的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2015-06-06
  • python3+PyQt5+Qt Designer實現(xiàn)堆疊窗口部件

    python3+PyQt5+Qt Designer實現(xiàn)堆疊窗口部件

    這篇文章主要為大家詳細介紹了python3+PyQt5+Qt Designer實現(xiàn)堆疊窗口部件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • python?包中的sched?事件調(diào)度器的操作方法

    python?包中的sched?事件調(diào)度器的操作方法

    sched模塊內(nèi)容很簡單,只定義了一個類。它用來最為一個通用的事件調(diào)度模塊,接下來通過本文給大家介紹python?包之?sched?事件調(diào)度器教程,需要的朋友可以參考下
    2022-04-04

最新評論