Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7a467dc

Browse files
committed
ex48 更复杂的用户输入
1 parent bb6b558 commit 7a467dc

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def scan(sentence):
2+
result = []
3+
words=sentence.split()
4+
length = len(words);
5+
while words:
6+
if words[0] in ['east','west','north','south']:
7+
result.append(('direction',words[0]))
8+
elif words[0] in ['go', 'kill', 'stop','eat']:
9+
result.append(('verb',words[0]))
10+
elif words[0] in ['door','bear','princess','cabinet']:
11+
result.append(('noun',words[0]))
12+
elif words[0] in['the','in','of','from','at','it']:
13+
result.append(('stop',words[0]))
14+
elif convert_number(words[0]):
15+
num = int(words[0])
16+
result.append(('number',num))
17+
else:
18+
result.append(('error',words[0]))
19+
words = words[1:]
20+
return result
21+
22+
def convert_number(s):
23+
try:
24+
return int(s)
25+
except ValueError:
26+
return None

0 commit comments

Comments
 (0)