[英]The movie game - skipping “The” at the beginning of a movie
The Movie Game is a game played by two people, and goes as follows. 电影游戏是由两个人玩的游戏,如下所示。 The first player names a movie. 第一个玩家为电影命名。 The second player then names a new movie whose title starts with the last letter of the movie the first player named. 然后,第二个播放器命名一个新电影,其标题以第一个播放器命名的电影的最后一个字母开头。
The game we will ignore the definite article "the" So if a player names the movie "Her Alibi" the next player can name the movie "The Incredibles," since the article "the" is ignored. 游戏我们将忽略明确的文章“the”因此如果玩家为电影“她的Alibi”命名,下一个玩家可以命名电影“The Incredibles”,因为文章“the”被忽略了。
How can I remove "The" from the movie title? 如何从电影标题中删除“The”?
def userInput(lastInput):
if lastInput == None:
return str(input("Enter a movie title: ")).lower()
if lastInput[:4] == "The ": # doesn't work
lastInput = lastInput[4:] # doesn't work
while True:
userChoice = str(input("Enter a movie title: ")).lower()
if userChoice[0] == lastInput[-1]:
return userChoice
else:
print("\nInvalid input, what would you like to do?")
instructions()
You can replace the Part of the string In The case You Mentioned THE with an empty string , use The following Code to Remove the word You want from the string 您可以替换字符串中使用空字符串提到的情况下部分,使用下面的代码从字符串删除您想要的单词
str="The Incredibles"
str.replace("The","")
Consider using regular expressions 考虑使用正则表达式
import re
a = r'^(\bthe\b)'
sts = ['the incredibles', 'theodore', 'at the mueseum', 'their words' ]
for i in sts:
b = re.sub(a,'', i)
print(b)
The regular expression I am using seems to work, but you might want to test more examples, using this link https://regex101.com/r/pX5sD5/3 我正在使用的正则表达式似乎有效,但您可能想要测试更多示例,使用此链接https://regex101.com/r/pX5sD5/3
You could do this: 你可以这样做:
if lastInput.lower().startswith("the "): lastInput = lastInput[4:]
Using the startswith()
method of strings, you can test for the first word directly (including the space that follows it). 使用字符串的startswith()
方法,您可以直接测试第一个单词(包括它startswith()
的空格)。 In order to support various capitalisation, turning the string to lowercase (using lower()
) allows you to only perform one test for any combinations of upper/lower case variants (eg "The ", "the ", "THE "). 为了支持各种大写,将字符串转换为小写(使用lower()
)允许您只对大小写变体的任何组合执行一次测试(例如“The”,“the”,“THE”)。
I also noticed that you are not applying this exclusion logic to the userChoice variable which is where I would have expected this to be used rather than on the lastInput variable. 我还注意到你没有将这个排除逻辑应用于userChoice变量,这是我期望使用它而不是lastInput变量的地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.