Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在python字符串中查找大小写混合的单词_Python 3.x - Fatal编程技术网

Python 3.x 在python字符串中查找大小写混合的单词

Python 3.x 在python字符串中查找大小写混合的单词,python-3.x,Python 3.x,一些背景: 3天前我从未尝试过编写任何程序,所以我是一个完全的初学者。我正在尝试学习python,我正在通过Grok learning上的一个免费模块学习有一个问题的答案,这个问题我基本上已经解决了,但无法完成。本模块是一门入门课程,因此我相信有一个相当简单的解决方案,但我在类似情况下看到的一切都让我不知所措 问题是:“机器人排成一行 机器人正在入侵(你的书面作品)!机器人正在潜入你的文本文件。编写一个程序,从用户那里读入一行文本,并打印出文本行中是否有机器人 如果robot一词出现在该行的所有

一些背景:

3天前我从未尝试过编写任何程序,所以我是一个完全的初学者。我正在尝试学习python,我正在通过Grok learning上的一个免费模块学习有一个问题的答案,这个问题我基本上已经解决了,但无法完成。本模块是一门入门课程,因此我相信有一个相当简单的解决方案,但我在类似情况下看到的一切都让我不知所措

问题是:“机器人排成一行

机器人正在入侵(你的书面作品)!机器人正在潜入你的文本文件。编写一个程序,从用户那里读入一行文本,并打印出文本行中是否有机器人

如果robot一词出现在该行的所有小写字母中,请打印出:该行中有一个小型robot

如果ROBOT一词出现在该行的所有大写字母中,请打印:该行中有一个大机器人

如果robot一词以大写和小写字母的任意组合出现在行中,请打印:行中有一个中等大小的robot。 否则,如果这些条件都不成立,你应该打印出来:这里没有机器人。 您的程序应如下所示:

Line: I'm baking chocolate robot brownies.
There is a small robot in the line. 
下面是另一个例子:

Line: Look at the rOBOt down the road.
There is a medium sized robot in the line.
如果字母robot出现在行中,但作为更大单词的一部分,则不应报告发现的任何robot。”

到目前为止,我的解决办法是:

line = input("Write something:")
lr = ("There is a small robot in the line.")
br = ("There is a big robot in the line.")
mr = ("There is a medium sized robot in the line.")
nr = ("No robots here.")
check1 = ("robot" in line)
check2 = ("ROBOT" in line)
lowcase = check1
upcase = check2
if(lowcase == True):
  print(lr)
elif(upcase == True):
  print(br)
else:
  print(nr)
请记住,我是一个完全的初学者,所以您的解决方案可能需要一些解释,并且可以随意评论我已经编写的代码,但到目前为止它似乎是有效的。感谢您抽出时间阅读所有这些内容和帮助。

使用或如何:




最困难的部分是robot出现在一个较大单词的某个部分,因为在字符串中查找“robot”很容易,但检查它的两侧是否有边界则比较困难

仅使用内置设备:

BOUNDARIES = " .,?!:;'\""

while True:
    string = input("Write something or hit Enter to quit: ")
    # This breaks the loop (quits the program) if the user hit
    # an Enter.
    if not string:
        break
    # First we look robot in the lowercased string.
    part = string.lower().partition("robot")
    # If a robot is in the string, the middle part of part
    # is going to be "robot". If not, it is an empty string.
    if not part[1]:
        print("No robots here.")
    # If the end of part[0] and the beginning of part[2] is
    # not in BOUNDARIES then we still have no robots there.
    elif ((not part[0] or not part[0][-1] in BOUNDARIES)
        and (not part[2] or not part[2][0] in BOUNDARIES)):
        print("No robots here.")
    # Now we look for small and big robots in the original
    # string.
    elif "robot" in string:
        print("There is a small robot in the line.")
    elif "ROBOT" in string:
        print("There is a big robot in the line.")
    # If we are here that is because of a medium robot.
    else:
        print("There is a medium sized robot in the line.")
使用可使其略短/更干净。但是,由于需要先导入
re
模块,因此程序的启动速度会稍慢一些:

import re
PATTERN = r"\b[rR][oO][bB][oO][tT]\b"

while True:
    string = input("Write something or hit Enter to quit: ")
    if not string:
        break
    search = re.search(PATTERN, string)
    if not search:
        print("No robots here.")
    elif search.group() == "robot":
        print("There is a small robot in the line.")
    elif search.group() == "ROBOT":
        print("There is a big robot in the line.")
    else:
        print("There is a medium sized robot in the line.")
试运行:

Write something or hit Enter to quit: I'm baking chocolate robot brownies.
There is a small robot in the line.
Write something or hit Enter to quit: Look at the rOBOt down the road.
There is a medium sized robot in the line.
Write something or hit Enter to quit: There's a strobotron at the concert.
No robots here.
Write something or hit Enter to quit: I have a robot.
There is a small robot in the line.
Write something or hit Enter to quit: The code on it's plate was: "ROBOT 142/1".
There is a big robot in the line.
Write something or hit Enter to quit:

这里有一种方法。。只是利用他们在课程中教给你的东西。 我知道这不是最聪明的方法,但它很简单,而且很有效!:)


您可以按以下方式尝试,但这涉及多个检查

s = "I'm baking chocolate roBOT brownies"
lower_text = 'robot'

normal_split = s.split()
lower_split = s.lower().split() 

if lower_text in normal_split:
    # check if lower case robot is present in normal split
    print('There is a small robot in the line.')
elif lower_text.upper() in normal_split:
    # check if upper case robot is present in normal split
    print('There is a big robot in the line.')
elif lower_text not in lower_split:
    # check if atleast any case robot is present in the split
    print('No robots here.')
else:
    # if none of the above matches then medium size robot
    print('There is a medium sized robot in the line.')
干杯

只是利用Grok讲座
可以使用以下方法在Python字符串中查找大小写混合的单词:

str1 = input("Line: ")
words = str1.split()
if "ROBOT" in words:
    print("There is a big robot in the line.")
elif "robot" in words:
    print("There is a small robot in the line.")
elif "robot" in str1.lower().split():
    print("There is a medium sized robot in the line.")
else:
    print("No robots here.")
import re
PATTERN = r"\b[rR][oO][bB][oO][tT]\b"

while True:
    string = input("Write something or hit Enter to quit: ")
    if not string:
        break
    search = re.search(PATTERN, string)
    if not search:
        print("No robots here.")
    elif search.group() == "robot":
        print("There is a small robot in the line.")
    elif search.group() == "ROBOT":
        print("There is a big robot in the line.")
    else:
        print("There is a medium sized robot in the line.")
Write something or hit Enter to quit: I'm baking chocolate robot brownies.
There is a small robot in the line.
Write something or hit Enter to quit: Look at the rOBOt down the road.
There is a medium sized robot in the line.
Write something or hit Enter to quit: There's a strobotron at the concert.
No robots here.
Write something or hit Enter to quit: I have a robot.
There is a small robot in the line.
Write something or hit Enter to quit: The code on it's plate was: "ROBOT 142/1".
There is a big robot in the line.
Write something or hit Enter to quit:
# Enter your code for "Robots in a line!" here.
str1 = input('Line: ')
words = str1.split()
if 'ROBOT' in words:
  print('There is a big robot in the line.')
elif 'robot' in words:
  print('There is a small robot in the line.')
elif 'robot' in str1.lower().split():
  print('There is a medium sized robot in the line.')
else:
  print('No robots here.')
s = "I'm baking chocolate roBOT brownies"
lower_text = 'robot'

normal_split = s.split()
lower_split = s.lower().split() 

if lower_text in normal_split:
    # check if lower case robot is present in normal split
    print('There is a small robot in the line.')
elif lower_text.upper() in normal_split:
    # check if upper case robot is present in normal split
    print('There is a big robot in the line.')
elif lower_text not in lower_split:
    # check if atleast any case robot is present in the split
    print('No robots here.')
else:
    # if none of the above matches then medium size robot
    print('There is a medium sized robot in the line.')
line = input("Line: ")
line1 = line.lower() #mixed case will be converted to lowercase
line = line.split() 
line1 = line1.split() #split line1 to see if the word robot exists

if "robot" in line:
    print("There is a small robot in the line.")
elif "ROBOT" in line:
    print("There is a big robot in the line.")
elif "robot" and "ROBOT" not in line and "robot" not in line1: #checking if lower case, upper case, and mixed case converted robot exist
    print("No robots here.")
else:
    print("There is a medium sized robot in the line.")
str1 = input("Line: ")
words = str1.split()
if "ROBOT" in words:
    print("There is a big robot in the line.")
elif "robot" in words:
    print("There is a small robot in the line.")
elif "robot" in str1.lower().split():
    print("There is a medium sized robot in the line.")
else:
    print("No robots here.")