gpt4 book ai didi

Python-如何检查一个单词是否有小写和大写

转载 作者:太空宇宙 更新时间:2023-11-04 04:35:13 24 4
gpt4 key购买 nike

我正在做一个学校项目。我对如何使 ( 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com