Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
bellcode
/
lesson13-1
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
7721f7d0
authored
Feb 19, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
19eb4278
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
17 deletions
diy.py
类和对象.py
diy.py
View file @
7721f7d0
with
open
(
r"C:\Users\10839\Desktop\sales_list.txt"
,
'r'
,
encoding
=
'utf-8'
)
as
file
:
#a=file.read()
#print(type(a)) #a为字符串类型
#print(type(a[3:5])) #取的是字符串里的数字12,也为字符串类型
a
=
file
.
readlines
()
#一次读取所有行并返回列表数据类型
print
(
type
(
a
))
#a的数据类型为列表
print
(
type
(
a
[
3
:
5
]))
#数字12的数据类型也为列表
#print(a)
with
open
(
r'C:\Users\10839\Desktop\sales_list.txt'
,
'r'
,
encoding
=
'utf-8'
)
as
file
:
a
=
file
.
readlines
()
final_sum
=
[]
for
i
in
a
:
data
=
i
.
split
()
#以空格分割列表i里的字符串(也就是用空格分割列表a的第一行)
#print(data[0]) #打印data列表的第一个字符串,也就是姓名
#print(data)#打印姓名后的所有字符串(义卖款)
data
=
i
.
split
()
sum
=
0
for
sales
in
data
[
1
:]:
#遍历每一个列表里所有的数字字符串
sum
+=
int
(
sales
)
#将每一个数字字符串转换为整型,然后加在sum里,即对每一行求和。
result
=
data
[
0
]
+
str
(
sum
)
#将每一行的人名+求和的结果赋值给result
print
(
result
)
#with open(r"C:\Users\10839\Desktop\sales_list.txt",'a',encoding='utf-8') as file:
#file.write(result)
for
sales
in
data
[
1
:]:
sum
+=
int
(
sales
)
result
=
data
[
0
]
+
str
(
sum
)
+
'
\n
'
final_sum
.
append
(
result
)
with
open
(
r'C:\Users\10839\Desktop\sales_list.txt'
,
'a'
,
encoding
=
'utf-8'
)
as
file
:
file
.
writelines
(
final_sum
)
类和对象.py
0 → 100644
View file @
7721f7d0
class
Hero
:
class
Hero
:
def
__init__
(
self
,
name
,
hp
,
attack
):
#self后的都是形参
self
.
level
=
1
self
.
name
=
name
self
.
hp
=
hp
#4、5、6行都是实例化属性
self
.
attack
=
attack
def
upgrade
(
self
):
self
.
level
=
self
.
level
+
1
self
.
hp
=
self
.
hp
+
50
self
.
attack
=
self
.
attack
+
20
yase
=
Hero
(
"亚瑟"
,
300
,
20
)
#实例化对象,300、80是实参,传参的数量顺序要和形参一致
houyi
=
Hero
(
"后羿"
,
200
,
30
)
#print(yase.name,yase.hp,yase.attack)
#print(houyi.name,houyi.hp,houyi.attack)
yase
.
upgrade
()
houyi
.
upgrade
()
print
(
yase
.
level
,
yase
.
hp
,
yase
.
attack
)
print
(
houyi
.
level
,
houyi
.
hp
,
houyi
.
attack
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment