Skip to content

Commit c845a09

Browse files
committed
添加 txt 文件,导入对象信息
1 parent 550e76e commit c845a09

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

josephus.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,15 @@ def append(self, target):
8282
if __name__ == '__main__':
8383

8484
ring = RingSort()
85-
ring.append(Person("Jorn", 25))
86-
ring.append(Person("Tom", 30))
87-
ring.append(Person("Jerry", 35))
88-
ring.append(Person("Mike", 40))
85+
f_txt = open("people.txt")
86+
for line in f_txt:
87+
if line.count(',') != 1:
88+
raise IOError("格式错误")
89+
line = line.strip('\n')
90+
name = line.split(',')[0]
91+
age = line.split(',')[1]
92+
ring.append(Person(name, age))
93+
f_txt.close()
8994

9095
try:
9196
start = int(input("\nPlease input a starting position:"))

people.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"Jorn", 25
2+
"Tom", 30
3+
"Jerry", 35
4+
"Mike", 40

0 commit comments

Comments
 (0)