Carbon Footprint Calculator

Totalling It Up

Next, we will get all the user’s responses and total up their emissions.

  • Now that we’re asking the user a question, we need to get their answer.
  • Add the following code to ask the user for their answer and store it in the variable named “response”.
  • Make sure the new code is in the right indentation.
  • For the question, we want to get the carbon amount from the relevant answer. Add the code to print out the amount of carbon.
  • Run your program. What happens when you try and put in a response? You probably get an error saying this:
  • The reason we get this error is because the input command gives us a string (text) instead of an integer (number). We need to convert our response into a number before we can use it.
  • Add this code and run your program again. That’s better!
  • Instead of showing the amount of carbon after each step, we want to keep a running tally and display it after we’ve asked all the questions.
  • First, we need to initialise our total to 0 before we start our question loop.

Instead of printing out the amount of carbon used after each question, we want to add it to the total.

  • Finally, we want to print this out after we’ve finished looping through the questions.
  • Note that we don’t have any spaces in front of line 16. The spaces are called ‘indenting’ and they tell Python that a command is in the loop. We don’t want our print statement to be in the loop, so we don’t want it to be indented. Make sure this code is right against the edge.
  • Run your program. You should be able to answer all of the questions and get an answer at the end.
  • Currently, the program is just spitting out a number. Let’s make that number a bit more user friendly.
  • Update your print line to the following to give your user some context to the number:
  • Run your program again. You should now get the total CO2 emitted displayed in a readable format.