Commit 71dfc4eb by BellCodeEditor

auto save

parent d80df9e9
class Hero:
def __init__(self, name, hp, attack):
self.level = 1
self.name = name # name:名字
self.hp= hp
self.attack= attack # attack:攻击
def upgrade(self):
self.level=self.level+1
self.hp = self.hp + 50
self.attack = self.attack + 4
houyi = Hero("后羿",240,23)
houyi.upgrade()
print("等级为:",houyi.level)
print("血量为:",houyi.hp)
print("攻击力为:",houyi.attack)
\ No newline at end of file
# 写入同学们的捐赠明细:'小兰:12本'、'小丽:11本'、'李文:9本'、'张伟:16本'
# 写入同学们的捐赠明细:'小兰:12本'、'小丽:11本'、'李文:9本'、'张伟:16本'
# 打开、创建文件,这里的路径要换成自己哦~
file1= open(r'C:\Users\EDZ\Desktop\test.txt','w',encoding='utf-8')
# 写入数据
file1.write('小兰:12本\n')
file1.write('小丽:11本\n')
file1.write('李文:9本\n')
file1.write('张伟:16本\n')
# 关闭文件
file1.close()
\ No newline at end of file
# 这里可以直接把左侧的test.txt.txt拖到控制台,松手,就是它的路径了。
# 打开文件有两种写法:
# 方法一:
file=open(r'c:\Users\EDZ\Desktop\test.txt','r',encoding='utf-8')
# 方法二:(注意结尾的英文冒号)
with open(r'c:\Users\EDZ\Desktop\test.txt','r',encoding='utf-8') as file:
\ No newline at end of file
# 在diy1的基础上选一种打开方式,建议选with open() as
with open(r'c:\Users\EDZ\Desktop\test.txt','r',encoding='utf-8') as file:
a=file.readlines()
#print(a)
for i in a:
print(i)
\ No newline at end of file
with open(r'c:\Users\EDZ\Desktop\test.txt','r',encoding='utf-8') as file:
a=file.readlines()
for i in a: #用for...in...把每一行的数据遍历
data=i.split() #把字符串切分成更细的一个个的字符串
print(data[0]) #打印出来看看,这个是所有的姓名
print(data[1:]) #打印出来看看,这个是所有的义卖款
\ No newline at end of file
with open(r'c:\Users\EDZ\Desktop\test.txt','r',encoding='utf-8') as file:
a=file.readlines()
for i in a: #用for...in...把每一行的数据遍历
data=i.split() #把字符串切分成更细的一个个的字符串
sum=0 #先把总钱数设为0
for sales in data[1:]: #遍历列表中第1个数据和之后的数据
sum=sum+int(sales)#然后依次加起来,但sales是字符串,所以要转换
result = data[0]+str(sum) #结果就是学生姓名和总义卖款
print(result) #打印看看
\ No newline at end of file
class Hero:
def __init__(self,name,hp,attack):
self.level = 1
self.hp = hp
self.attack = attack
yase = Hero("亚瑟",300,20)
houyi = Hero("后羿",240,23)
print(yase.hp)
print(houyi.attack)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment