简体   繁体   English

如何在破折号前打印一个单词?

[英]How do you print a word before a dash?

I am making a single player guessing game on capital cities, where the country and the first letter of the capital city are displayed and the user has 2 guesses to input the correct answer.我正在做一个首都城市的单人猜谜游戏,其中显示国家和首都的第一个字母,用户有2次猜测输入正确答案。

In an external .txt, I have the countries and cities stored, where they are separated by a '-'.在外部 .txt 中,我存储了国家和城市,它们以“-”分隔。 Here is an example of how they are formatted:以下是它们如何格式化的示例:

England - London英国 - 伦敦

France - Paris法国 - 巴黎

India - New Dehli印度 - 新德里

Say the 2nd line was randomly selected, I want the program to output this:假设第二行是随机选择的,我希望程序输出:

"The country is France and the first letter of the capital city is P. Try and guess!" “国家是法国,首都的第一个字母是P。猜猜看!”

Any help would be appreciated!任何帮助,将不胜感激! :) :)

so random text is the chosen text.所以随机文本是选择的文本。 You then split it by ' - ' and that creates a list like this ['France', 'Paris'] so then using the 'f' string you put in those variable but remember that they are a list so to access the country you need to access list index 0 which is first item in list like this random_text[0] for the first letter in word 'Paris' you first access the word like this random_text[1] which is the second item in the list and then you access that items' first character like this random_text[1][0] and print it out然后你用' - '分割它​​并创建一个像这样的列表['France','Paris'],然后使用你放在这些变量中的'f'字符串,但请记住它们是一个列表,以便访问您所在的国家需要访问列表索引 0,它是列表中的第一项,就像这个 random_text[0] 对于单词“Paris”中的第一个字母,你首先访问像这个 random_text[1] 这样的单词,它是列表中的第二个项目,然后你访问该项目的第一个字符像这样 random_text[1][0] 并将其打印出来

random_text = 'France - Paris'
random_text = random_text.split(' - ')
print(f'The country is {random_text[0]} and the first letter of the capital city is {random_text[1][0]}. Try and guess!')

Because you know what's your data looks like (XXX - YYY), a simple split by dash and white spaces will do:因为你知道你的数据是什么样子的 (XXX - YYY),一个简单的破折号和空格分割就可以了:

selected = "England - London"
country, city = selected.split(" - ")
print(f"The country is {country} and the city is {city}")

Use split.(), it's really useful in sperating strings.使用split.(),它在拆分字符串时非常有用。 The code:编码:

city = ["England - London","France - Paris","India - New Dehli"]
random_guess = city.split(' - ')

If you want to print random country and it's capital city, you'll need to import random如果要打印随机国家和首都,则需要导入 random

import random
city = ["England - London","France - Paris","India - New Dehli"]
random_guess = random.choice(city).split(' - ')
print("The country is",random_guess[0],"and the first letter of the capital city 
is",random_guess[1][0])

You can later add more element into the city list.您可以稍后将更多元素添加到城市列表中。

This is my solution.这是我的解决方案。

import re

exp = re.compile(r"(?P<country>\w+)[ ]*-[ ]*(?P<city>\w+)")


def __test(text):
    match = exp.match(text)
    print("The country is {country} and the city is {city}".format(**match.groupdict()))
    
    
__test("England - London")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM