04.lecture.menti.solutions

_澳门葡京手机版app下载

Exercise 1: Looping (1)

a) What is this code trying to achieve?

total = 0
for number in range(10):
   total = total + (number + 1)
print(total)
  1. summing all numbers from 1 to 10
  2. summing all numbers from 1 to 9
  3. summing all numbers from 0 to 10
  4. summing all numbers from 1 to 11
Answer. The first.

Solution.

  1. Correct: range(10) goes through the numbers 0 to 9, including, but the (number + 1) changes that to 1 to 10
  2. Maybe you thought range(10) goes from 1 to 9
  3. You may have missed the (number + 1)
  4. Maybe you thought range(10) goes to 10, including

Exercise 2: Looping (2)

a) What can fill in the blank?

genus = "Arabidopsis"
species = ["suecica", "thaliana" , "arenosa", "neglecta", "halleri", "lyrata"]

for ______ in species:
  print(genus, ______)
  1. species
  2. specie
  3. name
  4. x
Answer. All except option 1. Any valid variable name can be used in a for loop, but not the same variable nam as the list you are looping over.

Exercise 3: Ranges

a) Fill in the blank.

for i in _________:
    print(i)

10.0
10.5
11.0
11.5
  1. range(10, 11.5, .5)
  2. range(10, 12, .5)
  3. arange(10, 11.5, .5)
  4. arange(10, 12, .5)
Answer. Option 4 is correct - provided provided the Numpy module is imported (e.g. by from pylab import *)
  1. range() cannot be used with a non-integer set size
  2. range() cannot be used with a non-integer set size
  3. the last elemt of the range is not included, so this would only print 10.0, 10.5, 11.0
  4. correct

Solution.

from pylab import *
for i in arange(10, 12, .5):
    print(i)

10.0
10.5
11.0
11.5

Publisert 12. sep. 2017 08:12 - Sist endret 12. sep. 2017 08:20