如何在python中打印相应的单词(例如r>right)

2024-05-16 01:54:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我在GrokLearning上,遇到了这个问题:

在拉力赛中,了解路线上的地形是很重要的。导航员读出用速记写的步调,这样司机就知道会发生什么。 我们为我们的老虎车机器人导航器开发了一个简化的pacenote系统,使用以下速记:

r – right

l – left

j – jump

s – straight

写一个程序,读入地形,打印出课程笔记。例如:

Terrain: rrsj

right

right

straight

jump
​

如果pacenotes包含一个未知的速记,我们的机器人导航器应该会惊慌失措并打印出来:Aaaaah!

Terrain: jrdssjkl

jump

right

Aaaaah!

straight

straight

jump

Aaaaah!

left

我真的不知道该怎么办,所以我还没试过。。。除了:

a = input("Terrain: ")

b = list(a)

我能帮忙吗?顺便说一下,这是我的第一篇帖子。。。 你知道吗


Tags: right机器人路线left地形司机jump速记
1条回答
网友
1楼 · 发布于 2024-05-16 01:54:06
# store the rule in a dict.
xdict = {'r' : 'right', 'l' : 'left', 'j' :'jump', 's':'straight'}
# input has type str, which can be iterated to check each char
string = input("Terrain: ")
for x in string:
    # check if the char exists in the dict.
    if x in xdict:
        print(xdict[x])
    else:
        print('Aaaaah!')

相关问题 更多 >