❶ 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()
運行結果