Python 我想让它打印hello_ch变量,但我找不到一种方法来打印中文翻译工具
所以我想在这个例子中把英语翻译成另一种语言,汉语 所以我希望你们能够输入一些术语,能够添加一个前缀来定义一个变量,并打印它的值 我的代码:Python 我想让它打印hello_ch变量,但我找不到一种方法来打印中文翻译工具,python,Python,所以我想在这个例子中把英语翻译成另一种语言,汉语 所以我希望你们能够输入一些术语,能够添加一个前缀来定义一个变量,并打印它的值 我的代码: 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:") h
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\u ch
变量,但我找不到这样做的方法。您希望使用dict
,而不是一堆类似命名的变量
words = {"hello": {"chinese": "你好"}}
term = input("Please enter a term to translate")
print(words[term][lang])
保持你的逻辑。我使用了
eval
函数来帮助打印字符串
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 ( eval( term+"_ch")) ##this prints out hello_ch