Step 7: Using variables

This question will always ask the same question (3 * 9). We can make this question more interesting by using variables to do any number times 9.

Let's copy our first_question.yaml file to first_question_with_variables.yaml and add variables. To do this we will:

  • Add a variable named a with a value of 3
  • Change the statement, answer and correct_answer so it uses the variable a instead of the number 3.
---
type: normal
statement: How much is 9 * {a}? # this changed
advice: You could calculate 10 * {a} and then subtract {a}. This yields {a} * 10 - {a} = {a * 10} - {a} = {a*9} # this changed
parts:
  - type: number_entry
    prompt: Enter your answer in the inputbox
    marks: 5 
    answer: a * 9 # this changed
    show_correct_answer: true
    show_feedback_icon: true
    display_correct_as_fraction: false
    allow_fractions: false
    fractions_must_be_reduced: false
    fractions_must_be_reduced_hint: false
    partial_credit_if_fraction_not_reduced: 0
    display_correct_in_style: &style english-plain
    allowed_notation_styles:
      - *style
    custom_marking: none
    steps: []
    steps_penalty: 0
    adaptive_marking: none
    part_name: none
variables:
  a: 3 # this changed
# All fields below are not imporant yet, but need to be specified. Rumbas doesn't use any internal default values.
builtin_constants: # all constants will be disabled
  e: false
  pi: false
  i: false
custom_constants: []
grouped_variables: none
variables_test:
  condition: ""
  max_runs: 1
functions: {}
preamble:
  css: ""
  js: ""
navigation:
  can_regenerate: false
  show_title_page: false
  confirm_when_leaving: false
extensions:
  jsx_graph: false
  stats: false
  geogebra: false
  eukleides: false
  chemistry: false
  download_text_file: false
  graphs: false
  linear_algebra: false
  linear_codes: false
  optimisation: false
  permutations: false
  polynomials: false
  quantities: false
  random_person: false
  written_number: false
  sqlite: false
  text: false
diagnostic_topic_names: []
resources: []
custom_part_types: []
rulesets: {}

Task

Copy the first_question.yaml file to first_question_with_variables.yaml and change the parts indicated in the file above.

Now we will create a new exam that uses this question. We will copy the first_exam.yaml file to first_exam_with_variables.yaml and add the new question to the questions field.

---
type: normal
name: First Exam
question_groups:
  - name: Group 1
    picking_strategy:
      type: all_ordered
    questions:
      - first_question.yaml
      - first_question_with_variables.yaml # We added this
navigation:
  mode: sequential
  start_password: none
  can_regenerate: true
  show_steps: false
  show_title_page: false
  confirm_when_leaving: false
  show_names_of_question_groups: true
  allow_printing: false
  can_move_to_previous: true 
  browsing_enabled: true
  show_results_page: never
  on_leave: 
    action: none
feedback:
  percentage_needed_to_pass: 50
  show_name_of_student: false
  show_current_marks: true
  show_maximum_marks: true
  show_answer_state: true
  allow_reveal_answer: true
  review:
    show_score: true
    show_feedback: true
    show_expected_answer: true
    show_advice: true
  advice: "Please practice more"
  intro: "An introductory text"
  feedback_messages: []
timing:
  duration_in_seconds: none 
  allow_pause: true 
  on_timeout: 
    action: none
  timed_warning: 
    action: none
locales: 
  - name: en 
    numbas_locale: en-GB
numbas_settings:
  theme: default

Task

Copy the first_exam.yaml file to first_exam_with_variables.yaml and add the question as indicated in the file above.

We can then compile all exams with the following command:

rumbas compile exams

And open the new exam in the browser by browsing to http://localhost:8000/en/exams/first_exam_with_variables/.

Task

Try answering the second question and see if it works.

Task

Also try the following things for the second question:

  • Reload the page and use the Reveal answers button to see the correct answer. You will see that the calculations in the marks and advice fields are calculated.
  • Try the Try another question like this one button. It won't do anything yet, because we are not using randomisation yet.

You can also find the exam in the online demo.