Tag: random

National 5 – SDD Predefined Functions

So today we were looking over predefined function. For the N5 course we have to be aware of RANDOM, ROUND, & LENGTH.

The RANDOM function picks an integer between two values.

import random
number=random.randint(1,20) # random(StartValue,EndValue)
print(number)

The ROUND function rounds a real number (floating point) to a fixed number of decimal points.

a=1.2 # 1
b=2.6 # 3
c=2.5 # 2
d=3.5 # 4
e=3.453444354

print(round(e,2)) # round(valueToRound,NoOfDecimalDigits)

The LENGTH function displays the number of characters in a String or the length of an Array.

name="Stratton was here"
numbers=[1,2,3,4,2,3,4,6,4,3,2,43]
print(len(name))
print(len(numbers))
print(numbers)

for index in range(len(numbers)):
    print(numbers[index])

for index in range(len(name)):
    print(name[index])

 

Higher – Rock Paper Scissors – Solution

So when we implemented the code we got something like this

#Mr Stratton

import random

#set up variables
random.seed
object=["Rock","Paper","Scissors"]
playerChoice=""
computerChoice=""
winner=""

#Get players choice
playerChoice=object[int(input("Rock - 1\nPaper - 2\nScissors - 3\n"))-1]

#Get computer choice
computerChoice=object[random.randint(0,2)]

#get winner
if playerChoice=="Rock":
    if computerChoice=="Paper":
        winner="Computer"
    if computerChoice=="Scissors":
        winner="Player"
    if computerChoice==playerChoice:
        winner="Draw"
        
if playerChoice=="Paper":
    if computerChoice=="Scissors":
        winner="Computer"
    if computerChoice=="Rock":
        winner="Player"
    if computerChoice==playerChoice:
        winner="Draw"
        
if playerChoice=="Scissors":
    if computerChoice=="Rock":
        winner="Computer"
    if computerChoice=="Paper":
        winner="Player"
    if computerChoice==playerChoice:
        winner="Draw"
        

    
#display winner
print("\n"*10)
print("Player threw",playerChoice)
print("Computer threw",computerChoice)

if winner=="Player":
    print("Player is the winner")
    
if winner=="Computer":
    print("Computer is the winner")
    
if winner=="Draw":
    print("Its a draw")


However, although it does follow the design from yesterday it isn’t very effcient.
Can you see a way to make it more effient?

#Mr Stratton

import random

#set up variables
object=["Rock","Paper","Scissors"]
playerChoice=""
computerChoice=""
winner="Draw"

#Get players choice
playerChoice=object[int(input("Rock - 1\nPaper - 2\nScissors - 3\n"))-1]

#Get computer choice
computerChoice=object[random.randint(0,2)]

#get winner
if playerChoice=="Rock":
    if computerChoice=="Paper":
        winner="Computer"
    elif computerChoice=="Scissors":
        winner="Player"

        
elif playerChoice=="Paper":
    if computerChoice=="Scissors":
        winner="Computer"
    elif computerChoice=="Rock":
        winner="Player"

        
elif playerChoice=="Scissors":
    if computerChoice=="Rock":
        winner="Computer"
    elif computerChoice=="Paper":
        winner="Player"

        
else:
    print("Error in player choice")
    
#display winner
print("\n"*10)
print("Player threw",playerChoice)
print("Computer threw",computerChoice)
if winner=="Player":
    print("Player is the winner")
elif winner=="Computer":
    print("Computer is the winner")
else:
    print("Its a draw")

Read more

Import Function

Python is a general purpose language which means that it can be used to solve many different tasks. However Python does not have a command for every single eventuality. So programmers have created document with lots of different functions, these are called module libraries.

The import command is used to import module libraries into python programs.

dice

The random module stores a number of functions, to get access to the function we want we must first import random then use dot notation to specify the function we want to call. So random.randint calls the randint function from the random module library we imported previously.

There are libraries to do many things and they all should come with documentation to make them easy to use. Like all python programs they are open source so you can have a look inside to see how they work. Here are some example

Scratch revision task

You have to create a program that generates a random number between 1 and 15. The user has to make as many guesses as they need to get the correct number. To make it easier on the user, the computer will tell them if each guess is higher or lower.

  • Task 1 – Design the program (Completed below)
  • Task 2 – Implement the program in scratch
  • Task 3 – Test the program

Read more