導航:首頁 > 編程語言 > python用戶輸出四則運算

python用戶輸出四則運算

發布時間:2022-07-27 00:22:33

python 二叉樹實現四則運算

#!/usr/bin/python#* encoding=utf-8s = "20-5*(0+1)*5^(6-2^2)" c = 0top = [0,s[c],0]op = [["0","1","2","3","4","5","6","7","8","9"],["+","-"],["*","/"],["^"]] def getLev(ch): for c1 in range(0, len(op)): for c2 in range(0, len(op[c1])): if (op[c1][c2]==ch): return c1 elif (len(ch)>1): match = 0 for c3 in range(0, len(ch)): if (getLev(ch[c3])>=0): match+=1 if (match==len(ch)):return c1 return -1

❷ Python程序設計小學四則運算測試機 (隨機函數舉例)

簡單寫了一下,你看看行不行,python3.X

importrandom
opr=['+','-','×','÷']
jg='0'
print('Input"0000"Quit')
whileTrue:
fh=random.randint(0,3)
n1=random.randint(1,10)
n2=random.randint(1,10)
rjg=0
iffh==0:
rjg=n1+n2
eliffh==1:
n1,n2=max(n1,n2),min(n1,n2)
rjg=n1-n2
eliffh==2:
rjg=n1*n2
eliffh==3:
n1,n2=max(n1,n2),min(n1,n2)
whilen1%n2!=0:
n1=random.randint(1,10)
n2=random.randint(1,10)
n1,n2=max(n1,n2),min(n1,n2)
rjg=int(n1/n2)

print(n1,opr[fh],n2,'=',end='')
jg=input()
ifjg=='0000':
break
sr=int(jg)
ifint(sr)==rjg:
print('right')
else:
print('error.theTightansweris',rjg)

❸ python怎麼進行小數的四則運算並且可以自己定義小數點位數

首先要明白什麼是四則運算,四則運算就是加減乘除
其次我想說的是,加減乘除不是誰發明的,這個東西不屬於發明,是發現的一種,這是和日常成活密切相關的,所以是屬於勞動人民發現的,因為他是一種自然規律,不是人類創造出來的。

❹ python 四則運算

A = int(input())
B = int(input())
print("%d + %d = %d"%(A,B,A+B))
print("%d - %d = %d"%(A,B,A-B))
print("%d * %d = %d"%(A,B,A*B))
print("%d / %d = %d"%(A,B,A/B))

❺ python怎樣設計實現讓用戶輸入數字然後對用戶輸入的數字進行四則運算

❻ python有關四則運算的問題

a = input('請輸入第一個數字:')

b = input('請輸入第二個數字:')

c = input('請輸入運算符號:')

try:

float(a)

float(b)

except:

print('輸入錯誤!')

exit()

if c not in ['+', '-', '*', '/']:

print('輸入錯誤!')

exit()

print('結果:{}'.format(eval('{}{}{}'.format(a,c,b))))

❼ 請問誰會python整數四則運算

演算法

1

用input函數讀入兩個字元串表示的整數a、b

2

用int函數將a、b轉為整型

3

用print函數格式化輸出

下面是一個例子供參考:

a,b=int(input()),int(input())

print("%d+%d=%d" %(a,b,a+b))

print("%d-%d=%d" %(a,b,a-b))

print("%d*%d=%d" %(a,b,a*b))

print("%d/%d=%d" %(a,b,a/b))

輸出

❽ python 從鍵盤輸入兩個數字 然後用一種算術四則運算

import re


class OPERATION(object):

def __init__(self, a, b,ys):
self.a = a
self.b = b
self.ys=ys

def addition(self):
result = self.a + self.b
print(' = %s' % result)
return result

def subtraction(self):
result = self.a - self.b

print(' = %s' % result)
return result

def multiplication (self):
result = self.a * self.b
print(' = %s' % result)
return result

def division(self):
if self.b==0:
return print("輸入有誤")
else:
result = self.a / self.b
print(' = %s' % result)
return result

def operation(self):
if self.ys is '+':
OPERATION.addition(self)
elif self.ys is '-':
OPERATION.subtraction(self)
elif self.ys is '*':
OPERATION. multiplication (self)
elif self.ys is '/':
OPERATION.division(self)
else:
print("暫時沒有實現這種運算")
def main():
print("請輸要計算的算式,如 a+b 按回車鍵查看結果,輸入exit退出")
while True:
str = input(">>")
if str =='exit':
break;
else:
ret = re.match(r'^(d+)([+-*/]+)(d+)', str)
if ret:
numa = int(ret.group(1))
operationalCharacter = ret.group(2)
numb = int(ret.group(3))
print(numa, operationalCharacter, numb,end='')
yunsuan = OPERATION(numa, numb,operationalCharacter)
yunsuan.operation()
else:
print("請檢查輸入是否正確")


if __name__=='__main__':
main()

運行結果

閱讀全文

與python用戶輸出四則運算相關的資料

熱點內容
伺服器有什麼危害 瀏覽:256
飢荒怎麼開新的獨立伺服器 瀏覽:753
文件夾變成了 瀏覽:560
linuxpython綠色版 瀏覽:431
怎麼下載小愛同學音箱app 瀏覽:554
python佔位符作用 瀏覽:76
javajdbcpdf 瀏覽:543
php網頁模板下載 瀏覽:192
python試講課pygame 瀏覽:409
安居客的文件夾名稱 瀏覽:677
家裡伺服器如何玩 瀏覽:451
網站源碼使用視頻 瀏覽:748
stc89c52單片機最小系統 瀏覽:452
郵件安全證書加密 瀏覽:416
雲伺服器如何訪問百度 瀏覽:279
常州電信伺服器dns地址 瀏覽:839
用小方塊製作解壓方塊 瀏覽:42
圖像壓縮編碼實現 瀏覽:68
特色功能高拋低吸線副圖指標源碼 瀏覽:71
西方哲學史pdf羅素 瀏覽:874