Coding Practice
Objective 5:
Understand how to use arithmetic operations and random numbers
In this objective you learn how to perform mathematical calculations using power, modulus and
integer division. Random numbers allow for some unpredictability which is useful for games.
Tasks
1. Try entering the following program to see how arithmetic operators work:
#Get user input
number1=int(input("Enter first number: "))
number2=int(input("Enter second number: "))
#Make calculations
power_of_result = number1 ** number2
division_result = number1 / number2
integer_division_result = number1 // number2
modulus_result = number1 % number2
#Output results
print()
print(number1,"to the power of",number2,"is",power_of_result)
print(number1,"divided by",number2,"is",division_result)
print(number1,"divided
by",number2,"is",integer_division_result)
print(number1,"divided by",number2,"has a remainder
of",modulus_result)
2. Try entering the following commands and see what happens:
import random
#Roll the dice
random_number = random.randint(1,6)
print("You rolled a",random_number)
Note import random at the top of the program. For Python to generate a random
number it needs the randint method which is not in the default set of commands Python
understands. Instead it is defined in a library of methods, called ‘random’. You can import
additional commands from libraries into your program.
3. Change the program so that it outputs a 10 sided dice.
4. Change the program so the user can choose to roll a 4, 6 or 12 sided dice.
Objective 5:
Understand how to use arithmetic operations and random numbers
In this objective you learn how to perform mathematical calculations using power, modulus and
integer division. Random numbers allow for some unpredictability which is useful for games.
Tasks
1. Try entering the following program to see how arithmetic operators work:
#Get user input
number1=int(input("Enter first number: "))
number2=int(input("Enter second number: "))
#Make calculations
power_of_result = number1 ** number2
division_result = number1 / number2
integer_division_result = number1 // number2
modulus_result = number1 % number2
#Output results
print()
print(number1,"to the power of",number2,"is",power_of_result)
print(number1,"divided by",number2,"is",division_result)
print(number1,"divided
by",number2,"is",integer_division_result)
print(number1,"divided by",number2,"has a remainder
of",modulus_result)
2. Try entering the following commands and see what happens:
import random
#Roll the dice
random_number = random.randint(1,6)
print("You rolled a",random_number)
Note import random at the top of the program. For Python to generate a random
number it needs the randint method which is not in the default set of commands Python
understands. Instead it is defined in a library of methods, called ‘random’. You can import
additional commands from libraries into your program.
3. Change the program so that it outputs a 10 sided dice.
4. Change the program so the user can choose to roll a 4, 6 or 12 sided dice.