2

Sorry if the question is off, but this is some simple code.

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)

This code will result in

quiz_lenght

But I want to join quiz_ and user_input, which is length, and result in quiz_lenght and display what is set for that string, which is 'This test is five minutes long'.

Is there any way of achieving this?

1 Answers1

3

You should implement this using a dictionary:

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 will try to find a value based on a key in the first argument. If it does not find that key (i.e. if the user does not input one of the suggested values), it will return the value in the second argument. This will prevent getting a KeyError.

brianpck
  • 8,084
  • 1
  • 22
  • 33