Question: Utilizing arithmetic operators and arrays: To this point in your programming you may assume that arithmetic operators are solely used for mathematical operations however there

  1. Utilizing arithmetic operators and arrays: To this point in your programming you may assume that arithmetic operators are solely used for mathematical operations however there are cool things you can do with them outside of basic math. An example of this is the modulus (%). Modulus can be used to access indices outside of the length of your array.

Consider the following array:

summonTypes = ["soldier", "archer", "peasant", "paladin"]

Although the array only has 4 elements, with the use of % you can summon more than 4 troops.

The following code provides an example of how this would work. But there is a catch! There is an error in the code below. Use your debugging skills to identify the error, correct it and get the proper output.

summonTypes = ["soldier", "archer", "peasant", "paladin"]

for i in range(10):

type = summonTypes[ i % summonTypes.length ]

hero.summon(type)

Correct code in python please

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!