Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将变量和字符串联接以获取变量_Python_Python 3.x - Fatal编程技术网

Python 将变量和字符串联接以获取变量

Python 将变量和字符串联接以获取变量,python,python-3.x,Python,Python 3.x,对不起,如果这个问题是关闭的,但这是一些简单的代码 quiz_description = 'A quiz to test your knowledge' quiz_type = 'This quiz is about social studies' quiz_length = 'This test is five minutes long' while 0 == 0: user_input = input('What do you want to know about the quiz

对不起,如果这个问题是关闭的,但这是一些简单的代码

quiz_description = 'A quiz to test your knowledge'
quiz_type = 'This quiz is about social studies'
quiz_length = 'This test is five minutes long'

while 0 == 0:
    user_input = input('What do you want to know about the quiz? (length|type|description)\n')

    print('quiz_' + user_input)
此代码将导致

quick\u length

但是我想加入
测验
用户输入
,即
长度
,并生成
测验长度
,并显示为该字符串设置的内容,即
“此测试有五分钟长”


有什么方法可以做到这一点吗?

您应该使用字典来实现这一点:

quiz = {
    'description': 'A quiz to test your knowledge',
    'type': 'This quiz is about social studies',
    'length': 'This test is five minutes long',
}

while 0 == 0:
    user_input = input('What do you want to know about the quiz? (length|type|description)\n')

    print(quiz.get(user_input, 'Please choose a correct attribute!')
dict.get
将尝试根据第一个参数中的键查找值。如果未找到该键(即,如果用户未输入其中一个建议值),它将在第二个参数中返回该值。这将防止获得
键错误