我正在做一个学校项目。 我对如何使 ( if ) 语句检测大写和小写字符感到困惑。 我尝试在 if 语句中使用“and”,但它太长了。 我当前的输入是:
a=input('Line: ')
if 'robot' in a:
print('There is a small robot in the line.')
elif ('robot'.upper()) in a:
print('There is a big robot in the line.')
elif b in a:
print('There is a medium robot in the line.')
else:
print('No robots here.')
不要介意(b)我只是在想一些我不知道如何解释的事情。 我正在寻找的输出示例如下:
There is a "robot" in the line
then it would print
'There is a robot in the line'
程序将检查大写和小写字符。 如果输入全部大写,它将打印出来 队伍里有一个大机器人 如果输入只有小写字母,那么它只会打印 队伍里有一个小机器人。 如果输入同时包含小写和大写,则会打印: 队列中有一个中型机器人。
最佳答案
您可以先将输入小写,然后再检查小写的 robot
是否在输入字符串中:
更改:
elif b in a:
至:
elif 'robot' in a.lower():
关于Python-如何检查一个单词是否有小写和大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51887864/