Day 4: Randomisation and Lists
Randomness
Python uses the Mersenne Twister, a pseudo-random number generator (PRNG) and is the most widely used PRNG.
If you go to https://www.askpython.com and search for pseudo-random numbers there are loads of examples and information. What we need to do is import the random module (which incorporates the Mersenne Twister algorithm. No need to invent the wheel - might as well use a module :) We can generate random integers, floating point and shed load of others. Lets try some:
Random Integers
random.randint(1, 10)
Random integer between 1 and 10, INCLUDING BOTH random.randrange(1, 10)
Random integer in the range 1 and 10 INCLUDING 1 EXCLUDING 10
Random Floating Points
random.uniform(1, 10)
Random floating-point between 1 and 10, INCLUDING BOTH
random.random()
Random floating-point between 0.0 and 1.0 INCLUDING 0.0 EXCLUDING 1.0
What if we WANT random floating points ABOVE 1 and EXCLUDES THE UPPER NUMBER??
Well what if we multiply the floating random.random()
which is between (0 and 1) with an integer above 1 say like 5?? That would work

SEED
The python pseudorandom number generator uses the current time stamp as the seed. But we can in fact change this seed to what we want it to be

EXERCISE - Heads or Tails



To check if your code is correct use these seeds for the following:
321 = Heads
435 = Tails
34567 = Tails
456 = Heads
Python Lists
These are very important as we will be using these many many times in Python. The python lists is called a "DATA STRUCTURE" which is a way of organising and storing data in Python We have already seen ways of storing SINGLE pieces of data using "variables"
Sometimes we want to store 'grouped' pieces of data, which has some sort of connection with each other. For example, you want to store all the States of the USA together as they are all related in the sense that they are all "united" even though different and thus making up the USA as we know it. Other cases, you might want to have "order' in your data, for example, you were storing all the people in a virtual queue, you don't want the last person jumping the virtual queue

Lists
So lists have "order" starting with the 1st item as being [0] with the second as [1] and so on We can also call on the last item in the list with a [-1], second to last [-2] etc


Manipulating Items in Existing Lists - See docs.python
Lets say we want to change a spelling of an item:
monty_python_cast [2] = "Graeme Chapmann"
print(monty_python_cast)

We want to "add" and item to the end of a list (or delete)
monty_python_cast.append("Carol Cleveland") -----adds to the END of the list monty_python_cast.remove("Terry Gilliam") -------removes the name etc
We want to extend a list with multiple items
monty_python_cast.extend(["Neil Innes", "Douglas Adams", "Connie Booth")] ---> adds to end
AGAIN- have a look at the documentation https://docs.python.org/3/tutorial/datastructures.html
Exercise - Who's paying?


So what will happen is that the program will ask for a bunch of names separated by a comma and then it will randomly select a name from the program to pay.
SOME NEW CODE !!
Split String Method - REMEMBER - The divider (in this case commas ans a space between the names, can be changed to what you want)


Actual Code for the Exercise




Nested Lists
For example there is a list called the "Dirty Dozen" whereby these fruits and vegetables still have pesticide residue on them even after they have been cleaned and washed etc and now available for consumption. This list is order are:

So from this list we can see it comprises of both Fruits and Veggies, and both of these lists belong to a bigger list of "high pesticide foods" called the "Dirty Dozen"
fruits = ["Strawberries", "Nectarines", "Apples", "Grapes", "Peaches", "Cherries", "Pears"]
vegetables = ["Spinach", "Kale", "Tomatoes", "Celery", "Potatoes"]
Here we can have lists, within lists - for example:
dirty_dozen = [ fruits, vegetables]
print(dirty_dozen)

SOME TIPS!!
Given this code: What will be printed out??
fruits = ["Strawberries", "Nectarines", "Apples", "Grapes", "Peaches", "Cherries", "Pears"]
fruits[-1] = "Melons"
fruits.append("Lemons")
print(fruits)
fruits [-1] = "Melons"
This statement DELETES "Pears" and REPLACES it with "Melons"fruits.append("Lemons")
This statement ADDS on "Lemons" AFTER the new "Melons"print(fruits)
["Strawberries", "Nectarines", "Apples", "Grapes", "Peaches", "Cherries", "Melons", "Lemons"]

Rock Paper Scissors

import random
rock = '''
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
'''
paper = '''
_______
---' ____)____
______)
_______)
_______)
---.__________)
'''
scissors = '''
_______
---' ____)____
______)
__________)
(____)
---.__(___)
'''
game = [rock, paper, scissors]
user_choice = int(input("What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors:\n"))
if user_choice >= 3 or user_choice < 0:
print("You entered an invalid number, you lose")
else:
print(game[user_choice])
computer_choice = random.randint(0, 2)
print(f"Computer chose:\n {game[computer_choice]}")
if user_choice == 0 and computer_choice == 2:
print("You Win!")
elif computer_choice == 0 and user_choice == 2:
print("You Lose")
elif computer_choice > user_choice:
print("You Lose")
elif user_choice > computer_choice:
print("You Win")
elif user_choice == computer_choice:
print("Its a DRAW !!")


Last updated
Was this helpful?