⑴ 编写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)))