External Exam Download Resources Web Applications Games Recycle Bin

Exercises B

question 1.py

for n in range(10):
  # write a line of code here that prints the value of n



question 2.py

for i in range(10,1,-2):
  print(i)

# this currently prints:
# 10
# 8
# 6
# 4
# 2

# modify it so it prints:
# 20
# 17
# 14
# 11
# 8



question 3.py

start = int(input("Enter num between 1 and 10 >> "))

for c in range(start, 20, 1):
    print(c)

# This currently counts from a user inputted number up to 20.

# Make it so that the user can also input the STOP value
# (so that it counts up to a user inputted number as well).



question IV.py

score = int(input("Enter score or -1 to exit >> "))
total = 0
while(score != -1):

    # write a line of code here to add the score to the total
    
    score = int(input("Enter score or -1 to exit >> "))

print(total)



question V.py

import random

dicevalue = -1
rolls = 0

while( dicevalue != 0 ):

    dicevalue = random.randint(1,6)

    print(dicevalue)

    rolls = rolls + 1

print("It took you " + str(rolls) + " rolls to roll a 6.")


# At the moment, the program loop is not ending
# when i roll a 6. Can you fix it?




  1. Finish the above examples, filling in the required code where necessary.

  2. Use a loop within a loop (either for or while loop doesn't matter) to create this pattern of 5 x 5 asterix symbols:

    *****
    *****
    *****
    *****
    *****

  3. For the previous question, allow the users to enter the number of rows and columns to create the pattern (so that the pattern will be variable, dependent on the user input).

  4. For the previous question, turn the pattern into a 'set of stairs' pattern:
    *
    **
    ***
    ****
    *****