一般来说,我对python和编程还比较陌生,目前正在Grok online上一门在线课程。目前我被困在第二道课上(机器人排成一排!)简而言之就是设计一个程序,读入一行文本并打印出robot这个词是否出现,尽管它必须弄清楚这个词是小写、大写还是混合大小写。到目前为止,我的解决方案是:
text = input('Line: ')
if 'robot' in text:
print('There is a small robot in the line.')
elif 'robot'.upper() in text:
print('There is a big robot in the line.')
elif 'robot' or 'ROBOT' != text.isupper() and not text.islower():
print('There is a medium sized robot in the line.')
else:
print('No robots here.')
另一件事是程序必须将这个单词作为一个单独的字符串来区分,所以它会为“robot”打印True,而为“strobotron”打印false。在
您的第二个
elif
语句应该是如果你想在一条线上完成所有这些。在
对于第二个要求,可以使用regex word boundary anchors:
^{pr2}$这样可以处理标点符号并避免匹配strobotron:
我假设您的输入最多包含一个机器人字符串。在
相关问题 更多 >
编程相关推荐