某些背景信息:
我从未尝试过在3天前编程任何东西,所以我是一个完全的初学者。我正在尝试学习python,我正在通过一个关于Grok学习的免费模块学习https://groklearning.com/courses/有一个问题的答案我已经大部分已经弄清楚但是无法完成。该模块是一个入门课程,所以我相信有一个相当简单的解决方案,但我见过的类似情况的一切都在我的头上。
问题是:"机器人在一条线上!
机器人正在入侵(你的书面作品)!机器人潜入你的文本文件。编写一个程序,读取用户的一行文本,并打印出文本行中是否有机器人。
如果机器人中的所有小写字母都出现机器人,则打印出来:行中有一个小机器人。
如果单词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.
如果字母机器人出现在该行中,但作为更大词的一部分,则不应报告找到的任何机器人。"
Line: There's a strobotron at the concert.
No robots here.
到目前为止我的解决方案:
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)
请记住我是一个完全的初学者,所以你的解决方案可能需要一些解释,并且可以随意批评我已编写的代码,但到目前为止它似乎有用。感谢您花时间阅读所有这些并提供帮助。
答案 0 :(得分:1)
如何使用str.lower
或str.casefold
:
>>> 'Look at the rOBOt down the road.'.lower()
'look at the robot down the road.'
>>> 'robot' in 'Look at the rOBOt down the road.' # check1
False
>>> 'ROBOT' in 'Look at the rOBOt down the road.' # check2
False
>>> 'robot' in 'Look at the rOBOt down the road.'.lower()
True
if lowcase:
print(lr)
elif upcase:
print(br)
elif 'robot' in line.lower():
print(mr)
else:
print(nr)
答案 1 :(得分:1)
最困难的部分是当机器人出现在一个更大的单词的一部分时,因为很容易在字符串中查找“机器人”,但更难以检查它的两侧是否有边界。
仅使用内置:
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.")
使用regular expressions使其变得更短/更清洁。但是,您的程序启动会慢一点,因为它需要先导入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:
答案 2 :(得分:0)
这是一种方法。只需使用他们在课程中教给你的东西。 我知道它不是最聪明的方式,但它简单而且有效! :)
# 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.')
答案 3 :(得分:0)
您可以尝试如下,但这涉及多次检查。
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.')
干杯!
答案 4 :(得分:0)
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.")
答案 5 :(得分:-1)
您可以使用以下内容在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.')