Python习题七 1.代码:
f = open('exercise7_1.txt','w')
for i in range(1,10):
for j in range(1,i+1):
f.write(str(j)+'x'+str(i)+'='+str(j*i)+' ')
f.write("\n")
f.close()
运行结果:
2.代码:
str1 = input("请输入字符串:")
f = open('exercise7_2.txt','w',encoding='utf-8')
length= len(str1)
f.write("您输入的字符串的为:"+str1+'\n')
f.write("长度为:"+str(length))
f.close()
运行结果为:
3.代码:
from openpyxl import Workbook
workbook = Workbook()
worksheet = workbook.active
worksheet['C5'] = "我喜欢编程"
workbook.save("exercise7_3.xlsx")
运行结果: Python习题八: 1.代码:
class Circle:
def __init__(self,radius):
self.radius=radius
def getArea(self):
return 3.1415*self.radius**2
def getLen(self):
return 3.1415*2*self.radius
for i in range(1,11):
t=Circle(i)
print("半径为%d的圆,面积:%.2f 周长:%.2f"%(t.radius,t.getArea(),t.getLen()))
运行结果:
2.运行结果应该为 inside fun,x,L 3 [9, 8] x,L: 5 [4, 1] inside fun,x,L 3 [4, 1, 8] x,L: 5 [4, 1, 8]
代码:
def fun(x,L=[9]):
x=3
L.append(8)
print("inside fun,x,L",x,L)
x=5
L=[4,1]
fun(x)
print("x,L:",x,L)
fun(x,L)
print("x,L:",x,L)
运行结果:
3.代码:
class Account:
def __init__(self,usernumber,money,rate):
self.__usernumber=usernumber
self.__money=money
self.__year_Rate=rate
def month_Rate(self):
return self.getyear_Rate() / 1200.0
def month_Interest(self):
return self.getmoney() * self.month_Rate()
def getMoney(self, x):
self.__money = self.getmoney() - x
def deposit(self, x):
self.__money = self.getmoney() + x
def getUsernumber(self):
return self.__usernumber
def getmoney(self):
return self.__money
def getyear_Rate(self):
return self.__year_Rate
def setyear_Rate(self, year_Rate):
self.__year_Rate = year_Rate
year_Rate = 4.5
usernumber = "998866"
money = 2000
count1 = Account(usernumber, money, year_Rate)
count1.deposit(150)
count1.getMoney(1500)
print("账号:", count1.getUsernumber())
print("余额:", count1.getmoney())
print("年利率:", str(count1.getyear_Rate()) + "%")
print("月利率:", str(count1.month_Rate() * 100) + "%")
print("月息:", count1.month_Interest())
运行结果:
4.代码:
class Timer:
def __init__(self):
pass
def getHour(self,hour):
return self.hour
def getMin(self,min):
return self.min
def getSec(self,sec):
return self.sec
def setHour(self,hour):
self.hour=hour
def setMin(self,min):
self.min=min
def setSec(self,sec):
self.sec=sec
def showTime(self):
print("当前时间为:"+str(self.hour)+":"+str(self.min)+":"+str(self.sec))
t2 = Timer()
t2.setHour(10)
t2.setMin(20)
t2.setSec(20)
t2.showTime()
运行结果:
|