python - 如何在破折号前打印一个单词?
问题描述
我正在制作首府城市的单人猜谜游戏,其中显示国家和首府城市的首字母,用户有 2 次猜测输入正确答案。
在外部 .txt 中,我存储了国家和城市,它们用“-”分隔。以下是它们如何格式化的示例:
英格兰 - 伦敦
法国 - 巴黎
印度 - 新德里
假设第二行是随机选择的,我希望程序输出:
“国家是法国,首都的第一个字母是P。试试猜吧!”
任何帮助,将不胜感激!:)
解决方案
这是我的解决方案。
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")
推荐阅读
- sql - 如果条件满足,则将列的值替换为不同行的同一列 bu 的值
- sharepoint-online - SharePoint Online 文档库列类型与其内容类型的列类型不匹配
- python - 绘制饼图不显示所有数据
- python-3.x - 如何让 Python 的语音识别模块接受我的 YouTube 搜索查询
- node.js - 会话不在实时服务器/生产上工作,但在本地工作
- salesforce - 使用流(Salesforce)向多个用户发送电子邮件
- javascript - 按下按钮后如何禁用它?反应原生
- php - 使用 redirect() 和 back() 函数将值传递给刀片
- python - 用另一组具有开始和结束索引集的数组对 NumPy 数组进行切片
- windows - git 命令在 Windows 命令行上工作,但在 Git Bash 终端中不工作