⑴ 編寫python程序,定義兩個變數a,b並賦值,輸出它們相加、相減、相乘、相除,求平均值的結果。
#這個還是比較簡單的
a = input("a=")
b = input("b=")
print("a+b=",a+b)
print("a-b=",a-b)
print("axb=",a*b)
print("a/b=",a/b)
print("平均值:",(a+b)/2)
⑵ 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的for循環如何2個變數
python中的for循環和c中的for循環意義不同,python中for循環是迭代,如果要加入條件,要在for中嵌套if條件語句,
如果循環設兩個變數的話可以這樣:
for
i
in
range(1,10):
for
j
in
range(2,100):
print(i,j)
或者利用元組
for
(i,j)
in
[(1,2),(2,3),(4,5)]:
print(i,j)
⑷ 自動化測試時需要使用python,請問在python之兩個變數交換數據的方法有哪些
x,y = y,x
實例如下:
⑸ python編寫計算兩個數的乘積函數,利用這個函數求從鍵盤輸入的三個數的乘積
importmath
defmulti(a,b):
returna*b
if__name__=="__main__":
a,b,c=map(int,input().split())#一行輸入用空格分割
print(multi(multi(a,b),c))
⑹ python階乘函數,實現:輸入兩個數num1和num2,計算兩個數的階乘並求和
def jc(num):
jg = 1
辯桐for i in range(1,num+1):
告攜jg *= i
襪灶伏return jg
num1 = int(input())
num2 = int(input())
print('{}!+{}!={}'.format(num1,num2,jc(num1)+jc(num2)))