-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
48 lines (40 loc) · 1.21 KB
/
main.py
File metadata and controls
48 lines (40 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import argparse
import json
from random import choice
def parse_data():
parser = argparse.ArgumentParser(
prog='Word Filling Game',
description='A simple game',
allow_abbrev=True
)
parser.add_argument('--file', '-f', help='the path of file', required=True)
parser.add_argument('--article','-a', help='choose the article you like')
args = parser.parse_args()
return args
def read_articles(file_name):
with open(file_name, 'r', encoding='utf-8') as f:
data = json.loads(f.read())
return data
def get_input(hints):
keys =[]
for hint in hints:
m = input(f'请输入{hint}')
keys.append(m)
return keys
def replace(article, keys):
for i in range(len(keys)):
article = article.replace(f'{{{{{i + 1}}}}}', f'{{{keys[i]}}}')
return article
if __name__ == "__main__":
args = parse_data()
data = read_articles(args.file)
articles = data["articles"]
if args.article:
for article in articles:
if args.article == article['title']:
art = article
else:
art = choice(articles)
keys = get_input(art['hints'])
fin = art['article']
print(replace(fin, keys))