def sumDigits(n):
ge = int(n) % 10
c = 0
for i in range(len(str(n))):
bai = int(n) // (10 * (10 ** i)) % 10
c += bai
sum = c + ge
print("这个整数之和是:%d"%sum)
sumDigits(12345)
"""
运行结果:
这个整数之和是:15
"""
"""
03
def display(num1,num2,num3):
list = [num1,num2,num3]
s = sorted(list)
print("给三位数字排序得{}".format(s))
if __name__ == "__main__":
a,b,c = map(int,input("请输入三个数字:"))
display(a,b,c)
"""
运行结果:
请输入三个数字:654
给三位数字排序得[4, 5, 6]
"""
def printChars():
for i in range(73,91):
print(chr(i),end=" ")
if i% 10 == 0:
print("\n")
printChars()
"""
运行结果:
I J K L M N O P
Q R S T U V W X Y Z
"""
06
def numberOfDaysInAYear(year):
for count in range(year,year+11):
if count % 4 == 0 and count % 100 != 0 or count % 400 == 0:
print("{}年有366天".format(count))
else:
print("{}年有365天".format(count))
numberOfDaysInAYear(2010)
"""
运行结果:
2010年有365天
2011年有365天
2012年有366天
2013年有365天
2014年有365天
2015年有365天
2016年有366天
2017年有365天
2018年有365天
2019年有365天
2020年有366天
"""
import math
class RegularPolygon(object):
def __init__(self,n=3,length=1,x = 0,y = 0):
self.__n = n
self.__length = length
self.__x = x
self.__y = y
def N(self):
return self.__n
def side(self):
return self.__length
def getPerimeter(self):
perimeter = self.__n * self.__length
return perimeter
def getArea(self):
area = round((self.__n * self.getPerimeter() * self.getPerimeter()) /( 4 * math.tan(math.pi/4)),2)
return area
def main():
Re = RegularPolygon(4,1,8,0)
a = Re.getPerimeter()
b = Re.getArea()
n = Re.N()
length = Re.side()
print("这是一个",n,"边形","边长是",length,"周长为",a,"面积为",b)
main()
05
class LinearEquation(object):
def __init__(self):
pass
def main(self):
a = float(input('a= '))
b = float(input('b= '))
c = float(input('c= '))
d = float(input('d= '))
e = float(input('e= '))
f = float(input('f= '))
self.isSolvable(a,b,c,d,e,f)
def isSolvable(self,a,b,c,d,e,f):
if a*d-b*c ==0:
print('此方程无解')
else:
x = (e*d-b*f)/(a*d-b*c)
y = (a*f-e*c)/(a*d-b*c)
print('x为:',x)
print('y为:',y)
if __name__ == "__main__":
linearequation = LinearEquation()
linearequation.main()
06
class LinearEquation(object):
def __init__(self):
pass
def main(self):
x1,y1=eval(input('请输入第一条直线的一个端点'))
x2,y2=eval(input('请输入第一条直线的二个端点'))
x3,y3=eval(input('请输入第二条直线的一个端点'))
x4,y4=eval(input('请输入第二条直线的二个端点'))
self.jisuan(x1,y1,x2,y2,x3,y3,x4,y4)
def jishuan(self,x1,y1,x2,y2,x3,y3,x4,y4):
a = y2-y1
b = x2*y1-x1*y2
c = x2-x1
d = y4-y3
e = x4*y3-x3*y4
f = x4-x3
self.gongshi(a,b,c,d,e,f)
def gongshi(self,a,b,c,d,e,f):
y = float(a*e-b*d)/(a*f-c*d)
x = float(y*c-b)/a
print('交点的坐标为:',(x,y))
if __name__ == "__main__":
linearequation = LinearEquation()
linearequation.main()