首页 > 解决方案 > 我希望它打印 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变量,但我找不到这样做的方法。

标签: python

解决方案


你想使用一个dict,而不是一堆类似命名的变量。

words = {"hello": {"chinese": "你好"}}

term = input("Please enter a term to translate")
print(words[term][lang])

推荐阅读