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
- 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started