python - 我希望它打印 hello_ch 变量但我找不到这样做的方法 中文翻译工具
问题描述
所以我想在这个例子中将英语翻译成另一种语言,中文。
所以我希望你能够输入一些术语并能够添加一个前缀来定义一个变量并打印它的值。
我的代码:
print("Hey there! Welcome to Max's Translation Tool!")
name = input('Please enter your name: ')
lang = input("Howdy " + name + "! What language would you like to translate to:")
hello = "Hello"
hello_ch = str("你好")
if lang == "chinese":
term = input("Please enter a term to translate:")
print(str(term + "_ch"))
我希望它打印hello_ch
变量,但我找不到这样做的方法。
解决方案
你想使用一个dict
,而不是一堆类似命名的变量。
words = {"hello": {"chinese": "你好"}}
term = input("Please enter a term to translate")
print(words[term][lang])
推荐阅读
- javascript - Summernote 上传图片:onImageUpload 和 sendFile 功能不起作用
- javascript - javascript删除重复项不起作用
- node.js - 如何将用户名(存储在 localstorage Token 中)传递给后端?(同步融合时间表)
- python - 用于对补丁进行分类的 TensorFlow 数据集管道
- php - PHP-FFMPeg 不在 CentOS 服务器上处理文件
- c++ - 将 c++ 向量转换为 char ** 而不会发生内存泄漏
- c# - 绕过全局查询过滤器
- c# - WPF 绑定到数据模板按钮列
- javascript - 无法将 InputBox 字符串值从 HTML 传递到 Google Apps 脚本
- php - 将 ID 值更改为数据库中的所有行 + 重命名所有文件夹