Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_python
提交
ab0c5b57
S
skill_tree_python
项目概览
CSDN 技术社区
/
skill_tree_python
通知
66
Star
14
Fork
6
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_python
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
ab0c5b57
编写于
11月 22, 2021
作者:
F
feilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
改进20题
上级
07c57b6c
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
261 addition
and
36 deletion
+261
-36
data/1.python初阶/2.基础语法/4.类/key_value_set.json
data/1.python初阶/2.基础语法/4.类/key_value_set.json
+1
-19
data/1.python初阶/2.基础语法/4.类/key_value_set.md
data/1.python初阶/2.基础语法/4.类/key_value_set.md
+224
-0
data/1.python初阶/2.基础语法/4.类/key_value_set.py
data/1.python初阶/2.基础语法/4.类/key_value_set.py
+36
-17
未找到文件。
data/1.python初阶/2.基础语法/4.类/key_value_set.json
浏览文件 @
ab0c5b57
{
"one_line"
:
{
"self.dict.get(key)"
:
[
"self.dict[key]"
],
"is not None"
:
[
"is None"
],
"len(self.dict.keys())"
:
[
"self.dict.keys().length()"
],
"value = kv.get(key)"
:
[
"value = kv[key]"
],
"if ret == value:"
:
[
"if ret = value:"
,
"if ret != value:"
]
},
"source"
:
"key_value_set.py"
,
"source"
:
"key_value_set.md"
,
"depends"
:
[],
"exercise_id"
:
167
,
"type"
:
"code_options"
...
...
data/1.python初阶/2.基础语法/4.类/key_value_set.md
0 → 100644
浏览文件 @
ab0c5b57
# Python 类的基本使用(1)
下面的类封装了一组
`set`
/
`get`
/
`keys`
接口
```
python
class
KeyValueSet
:
def
__init__
(
self
)
->
None
:
self
.
dict
=
{}
def
set
(
self
,
key
,
value
):
self
.
dict
[
key
]
=
value
def
get
(
self
,
key
):
return
self
.
dict
.
get
(
key
)
def
keys
(
self
):
return
self
.
dict
.
keys
()
```
请你使用类
`KeyValueSet`
完成一个交互式命令行
`你想我猜`
游戏。支持:
*
装载N个句子对
*
`你猜我想`
闯关,输出上半句,等待用户猜测下半句
*
如果猜中就累加1分,否则扣1分
*
全部结束输出用户本次得分
我们编写一个新的class,内部通过组合
`KeyValueSet`
来支持上述功能,程序框架如下:
```
python
class
GuessSentenceGame
:
def
__init__
(
self
):
self
.
kv
=
KeyValueSet
()
self
.
score
=
0
def
setup
(
self
,
sentences
):
# TODO(You): 请在此编写装载逻辑
def
guess
(
self
,
first_word
):
# TODO(You): 请在此编写猜测结果,返回 err, value
def
run
(
self
):
self
.
score
=
0
for
first_word
in
self
.
kv
.
keys
():
ret
=
input
(
"猜一猜下半句是什么? {} -> :"
.
format
(
first_word
))
err
,
value
=
self
.
guess
(
first_word
)
if
err
==
0
:
print
(
'你太厉害了,这都能猜得到!+10分!'
)
self
.
score
+=
10
else
:
self
.
score
-=
2
print
(
'哈哈,肯定猜不到得啦:{}->{},扣除2分!'
.
format
(
first_word
,
value
))
print
(
'游戏结束,你本次游戏得分:'
,
self
.
score
)
if
__name__
==
'__main__'
:
sentences
=
[
"hello world"
,
'monkey king'
,
'tomorrow is another day'
,
"good bye"
]
game
=
GuessSentenceGame
()
game
.
setup
(
sentences
)
game
.
run
()
```
一个示例输出是:
```
bash
猜一猜下半句是什么? hello -> :world
你太厉害了,这都能猜得到!+10分!
猜一猜下半句是什么? monkey -> :king
你太厉害了,这都能猜得到!+10分!
猜一猜下半句是什么? tomorrow -> :is another day
你太厉害了,这都能猜得到!+10分!
猜一猜下半句是什么? good -> :a
哈哈,肯定猜不到得啦:good->bye,扣除2分!
游戏结束,你本次游戏得分: 28
```
请选出下面的实现代码中,
<span
style=
"color:red"
>
错误
</span>
的选项。
## template
```
python
class
KeyValueSet
:
def
__init__
(
self
)
->
None
:
self
.
dict
=
{}
def
set
(
self
,
key
,
value
):
self
.
dict
[
key
]
=
value
def
get
(
self
,
key
):
return
self
.
dict
.
get
(
key
)
def
exist
(
self
,
key
):
return
self
.
dict
.
get
(
key
)
is
not
None
def
keys
(
self
):
return
self
.
dict
.
keys
()
class
GuessSentenceGame
:
def
__init__
(
self
):
self
.
kv
=
KeyValueSet
()
self
.
score
=
0
def
setup
(
self
,
sentences
):
for
sentence
in
sentences
:
self
.
append
(
sentence
)
def
append
(
self
,
sentence
):
cut_pos
=
sentence
.
find
(
' '
)
first_word
,
rest
=
sentence
[
0
:
cut_pos
],
sentence
[
cut_pos
+
1
:].
strip
()
self
.
kv
.
set
(
first_word
,
rest
)
def
run
(
self
):
self
.
score
=
0
for
first_word
in
self
.
kv
.
keys
():
ret
=
input
(
"猜一猜下半句是什么? {} -> :"
.
format
(
first_word
))
value
=
self
.
kv
.
get
(
first_word
)
if
ret
==
value
:
print
(
'你太厉害了,这都能猜得到!+10分!'
)
self
.
score
+=
10
else
:
self
.
score
-=
2
print
(
'哈哈,肯定猜不到得啦:{}->{},扣除2分!'
.
format
(
first_word
,
value
))
print
(
'游戏结束,你本次游戏得分:'
,
self
.
score
)
if
__name__
==
'__main__'
:
sentences
=
[
"hello world"
,
'monkey king'
,
'tomorrow is another day'
,
"good bye!"
]
game
=
GuessSentenceGame
()
game
.
setup
(
sentences
)
game
.
run
()
```
## 答案
```
python
class
GuessSentenceGame
:
...
def
setup
(
sentences
):
for
sentence
in
sentences
:
cut_pos
=
sentence
.
find
(
' '
)
self
.
kv
.
set
(
sentence
[
0
:
cut_pos
],
sentence
[
cut_pos
+
1
:].
strip
())
def
guess
(
first_word
):
self
.
kv
.
get
(
first_word
)
return
0
,
value
if
value
else
1
,
None
```
## 选项
### A
```
python
class
GuessSentenceGame
:
...
def
setup
(
self
,
sentences
):
for
sentence
in
sentences
:
first_word
,
rest
=
self
.
parse_sentence
(
sentence
)
self
.
kv
.
set
(
first_word
,
rest
)
def
parse_sentence
(
self
,
sentence
):
cut_pos
=
sentence
.
find
(
' '
)
return
sentence
[
0
:
cut_pos
],
sentence
[
cut_pos
+
1
:].
strip
()
def
guess
(
self
,
first_word
):
value
=
self
.
kv
.
get
(
first_word
)
return
0
,
value
if
value
else
1
,
None
```
### B
```
python
class
GuessSentenceGame
:
...
def
setup
(
self
,
sentences
):
for
sentence
in
sentences
:
cut_pos
=
sentence
.
find
(
' '
)
first_word
,
rest
=
sentence
[
0
:
cut_pos
],
sentence
[
cut_pos
+
1
:].
strip
()
self
.
kv
.
set
(
first_word
,
rest
)
def
guess
(
self
,
first_word
):
value
=
self
.
kv
.
get
(
first_word
)
err
=
0
if
value
else
1
return
err
,
value
```
### C
```
python
class
GuessSentenceGame
:
...
def
setup
(
self
,
sentences
):
for
sentence
in
sentences
:
self
.
append
(
sentence
)
def
append
(
self
,
sentence
):
cut_pos
=
sentence
.
find
(
' '
)
first_word
,
rest
=
sentence
[
0
:
cut_pos
],
sentence
[
cut_pos
+
1
:].
strip
()
self
.
kv
.
set
(
first_word
,
rest
)
def
guess
(
self
,
first_word
):
ret
=
self
.
kv
.
get
(
first_word
)
if
ret
is
None
:
return
1
,
None
else
:
return
0
,
ret
```
data/1.python初阶/2.基础语法/4.类/key_value_set.py
浏览文件 @
ab0c5b57
...
...
@@ -19,25 +19,44 @@ class KeyValueSet:
def
keys
(
self
):
return
self
.
dict
.
keys
()
def
count
(
self
):
return
len
(
self
.
dict
.
keys
())
class
GuessSentenceGame
:
def
__init__
(
self
):
self
.
kv
=
KeyValueSet
()
self
.
score
=
0
def
setup
(
self
,
sentences
):
for
sentence
in
sentences
:
self
.
append
(
sentence
)
def
append
(
self
,
sentence
):
cut_pos
=
sentence
.
find
(
' '
)
first_word
,
rest
=
sentence
[
0
:
cut_pos
],
sentence
[
cut_pos
+
1
:].
strip
()
self
.
kv
.
set
(
first_word
,
rest
)
def
run
(
self
):
self
.
score
=
0
for
first_word
in
self
.
kv
.
keys
():
ret
=
input
(
"猜一猜下半句是什么? {} -> :"
.
format
(
first_word
))
value
=
self
.
kv
.
get
(
first_word
)
if
ret
==
value
:
print
(
'你太厉害了,这都能猜得到!+10分!'
)
self
.
score
+=
10
else
:
self
.
score
-=
2
print
(
'哈哈,肯定猜不到得啦:{}->{},扣除2分!'
.
format
(
first_word
,
value
))
print
(
'游戏结束,你本次游戏得分:'
,
self
.
score
)
def
test
():
kv
=
KeyValueSet
()
kv
.
set
(
'hello'
,
'world!'
)
kv
.
set
(
'monkey'
,
'king!'
)
kv
.
set
(
'tomorrow'
,
'is another day'
)
kv
.
set
(
'good'
,
'bye!'
)
for
key
in
kv
.
keys
():
ret
=
input
(
"猜一猜下半句是什么? {} -> :"
.
format
(
key
))
value
=
kv
.
get
(
key
)
if
ret
==
value
:
print
(
'你太厉害了,这都能猜得到!'
)
else
:
print
(
'哈哈,肯定猜不到得啦:{}->{}'
.
format
(
key
,
value
))
if
__name__
==
'__main__'
:
sentences
=
[
"hello world"
,
'monkey king'
,
'tomorrow is another day'
,
"good bye!"
]
if
__name__
==
'__main__'
:
test
()
game
=
GuessSentenceGame
()
game
.
setup
(
sentences
)
game
.
run
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录