Homework tasks

1 Homework

Homework 1 - Programming Basics

  • During week 1 you have played a text based game and had a look at the Python code.
  • During week 2, you have learned and practiced using while loops.
  • For this assignment
    1. look at the Python game file in week 1 and identify the lines of code that,:
      1. ask user for input
      2. output some information
    2. Explain how this line of code works:
      random.randrange(0,10)
      
    3. The follow code is supposed to output numbers from 0 to 9. But it has an error in it. Please identify the error.
while x < 10:
	x = 0
	print (x)
	x++

Homework 2 - The range command

1
2
for I in range(5):
    print("Welcome to the range")
  • The above code is a for loop - that is, the command indented underneath the for i in range(5) will be repeated. In this case, the Welcome to the range will be printed 5 times. Why?
  • It is becasue the number of times a loop repeats is specified by the number given in the range(5).
  • Range command works like this:
    1. range(upToNumber) - it produce a sequence of numbers from 0 up to BUT NOT equal to the upToNumber. For example, range(5) will produce: 0,1,2,3,4, therefore, there are 5 numbers, for each number the loop going through, it prints out "Welcome to the range".
    2. range(startNumber, upToNumber) - it produces a sequence of numbers from startNumber to BUT NOT equal to the upToNumber. For example, range(3,8) will produce: 3,4,5,6,7
    3. range(startNumber, upToNumber, increaseBy)- it produces a sequence of numbers from startNumber to BUT NOT equal to the upToNumber, each time the next number increase by the number specified by increaseBy. For example, range(2,10,2) will produce: 2,4,6,8.
  • Your homework:
    1. What numbers are produced in this loop:
      1
      2
      for i in range(10, 20, 2):
          print(i)
      
    2. What numbers are produced in this loop:
      1
      2
      for i in range(100, 40, -5):
          print(i)
      
    3. write a for loop that will print out all positive whole numbers (integers) up to 100 that are divisible by 5.

Homework 3 - Literacy Task

  • coming soon