Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

In an investigate task you take working code apart to see how each piece contributes to the result. Answer every question for yourself first. You can edit the code and run it to check. Only then open the answer.

The program below works out how much energy a generator makes, and how much of it is wasted because it is not enough to fill one more battery. Run it a few times before you start: once with 2 and then 5, once with 3 and then 10, and once with 5 and then 100.

print("Enter power level (1-5):")
power = int(input())

total_energy = power ** 4

print("Enter battery capacity:")
capacity = int(input())

remaining_energy = total_energy % capacity

print("Total energy generated: " + str(total_energy))
print("Energy wasted: " + str(remaining_energy))

What data type is returned by the input() function on line 2 before it is cast?

Answer

A string.

input() always collects what the user typed as text, even when it looks like a number. The int() wrapped around it is what turns that text into a whole number, before = stores it in power.

What specific mathematical operation does ** 4 perform on line 4?

Answer

It raises power to the power of 4: power multiplied by itself four times.

power ** 4 is the same as power * power * power * power. The ** operator is exponentiation, and the number after it is how many times the number before it is used in the multiplication.

If the user enters 2 for the power level, what specific integer value will be assigned to the total_energy variable?

Answer
16

2 to the power of 4 is 2 times 2 times 2 times 2, which is 16. Watch that it is not 8: 2 ** 4 is not the same as 2 * 4.

If total_energy is 16 and the user enters 5 for the battery capacity, what value will be stored in remaining_energy?

Answer
1

5 goes into 16 three whole times, which uses up 15, and 1 is left over. % gives the part that is left over, so 16 % 5 is 1.

Why must the capacity variable be converted to an integer on line 7 before line 9 can execute successfully?

Answer

Because modulo is a calculation, and Python cannot calculate with text.

Take the int() away from line 7 and capacity holds a string, such as "5". Line 9 then asks for the remainder of a number divided by a piece of text, and the program stops with:

TypeError: unsupported operand type(s) for %: 'int' and 'str'

Try it: remove the int() in the playground and run the program again.

What is the purpose of the % operator on line 9 in the context of filling the batteries?

Answer

It works out how much energy is left over once every battery that can be filled completely has been filled.

With 16 units of energy and batteries that hold 5, three batteries are filled and use up 15 units. The last unit cannot fill a fourth battery, so it is wasted, and that is what 16 % 5 calculates. The number of full batteries would be total_energy // capacity, which is 3; the program does not print that, only the remainder.

Help! My code doesn't work!

Make sure that you check for the following things:

  • You have typed the variable name exactly the same, capital letters included, every time.
  • You have used a single = for assignment.
  • The two stars in ** are written next to each other, with no space between them.
  • % gives the remainder, not a percentage.
  • Input that has to be calculated with is wrapped in int().
  • Every number that is concatenated into a print is wrapped in str().