CodeNewbie Community 🌱

Vishnubhotla Bharadwaj
Vishnubhotla Bharadwaj

Posted on • Originally published at bharadwaj.hashnode.dev on

Day 4, Randomization and Python Lists

Yes, From the title you can understand today's concept is about Randomization and Python lists. At the end of the day, we can create a simple Rock-Paper-Scissors game.

Randomization

It is a very important topic when we need to create a computer program with unpredictability.

To generate a random number first, we should import it

import random

Enter fullscreen mode Exit fullscreen mode

To generate integer random numbers

random.randint(1,10)

Enter fullscreen mode Exit fullscreen mode

Here random numbers are generated between 1 to 10, including 1 and 10.

Random float numbers are generated by

random.random()

Enter fullscreen mode Exit fullscreen mode

This statement generates random numbers between 0.0 to 1.0, including 0.0 but excluding 1.0. But, to generate numbers other than the index of 0, multiply the random number by any integer.

Remember the love calculator we made on day 3, the code for that can be reduced greatly as

import random
ls = random.randint(1,100)
print(f"Your love score is {ls}")

Enter fullscreen mode Exit fullscreen mode

This program simply generates a random number between 1 and 100.

Now, let's do a simple Heads or Tails program ( Virtual coin toss program)

Seed: One of the easiest ways for making a computer to generate some random numbers or random sequences.

import random
test_seed = int(input("Create a seed number: "))
random.seed(test_seed)
random_number = random.randint(0,1)
if random_number == 1:
    print("Heads")
else:
    print("Tails")

Enter fullscreen mode Exit fullscreen mode

Normally, random number generator in python uses the current timestamp as the seed, but we can in fact change the seed to something we want to be like this

random.seed(123)
# random numbers generated by this always will be the same value

Enter fullscreen mode Exit fullscreen mode

Python lists

-> It is the way of organizing and storing data in python.

-> Just like data structure.

-> Any data types can be stored.

states_of_india = ["AP", "TN", "TS"]
print(states_of_india[0]) #1
print(states_of_india[-1]) #2
states_of_india[1] = "Jk" #3
print(states_of_india) #4
states_of_india.append("HR") #5
states_of_india.extend(["AS", "KL", "KR"]) #6
print(states_of_india) #7

Enter fullscreen mode Exit fullscreen mode
  1. Output is AP
  2. Output is TS
  3. Changes value of TN to JK
  4. Output will be ["AP", "JK", "TS"]
  5. ["AP", "JK", "TS", "HR"] adds HR at the last of the list
  6. ["AP", "JK", "TS", "HR", "AS", "KL", "KR"] adds new list at end of list.

It's better to refer to the documentation of lists.

One gets the doubt why the 1st element is 0, because of the offset from the right side of the equality sign. As the 1st element has the offset 0.

Negative numbers are also used which starts counting from the end of the list.

Who is playing the bill program

import random
test_seed = int(input("Create a seed number: "))
random.seed(test_seed)
name = input("Enter the names to pay the bill seperated by comma ")
names = name.split(",")
print(names)
print(len(names))
choice = random.randint(0,len(names)-1)
pay_bill = names[choice]
print(pay_bill + " is going to buy meal today.")

Enter fullscreen mode Exit fullscreen mode

split function divides the string into certain components based on some divider. And finally forms a list.

Sample program using lists

row1 = ["⬜️","⬜️","⬜️"]
row2 = ["⬜️","⬜️","⬜️"]
row3 = ["⬜️","⬜️","⬜️"]
map = [row1, row2, row3]
print(f"{row1}\n{row2}\n{row3}")
position = input("Where do you want to put the treasure? ")
n = int(position[0])
m = int(position[1])
map[m -1][n-1] = "X"
print(f"{row1}\n{row2}\n{row3}")

Enter fullscreen mode Exit fullscreen mode

Index errors: If the index is out of bounds i.e, it exceeds the scope of list.

Finally, That's the end of day-4 and the final project of the day Rock-Paper-Scissors program is here

import random
rock = '''
    _______
--------' ____ )
      ( _____ )
      ( _____ )
      ( ____ )
--------.__(___)
'''

paper = '''
    _______
--------' ____)____
          ______ )
          _______ )
         _______ )
--------. __________ )
'''

scissors = '''
    _______
--------' ____)____
          ______ )
       __________ )
      ( ____ )
--------.__(___)
'''
game_images = [rock, paper, scissors]
get_input = int(input("What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors "))
print(game_images[get_input])
comp_random = random.randint(0,2)
print("Computer Choice: ")
print(game_images[comp_random])
if get_input >= 3 or get_input < 0:
  print("Invalid number, You lose")
elif get_input ==0 and comp_random ==2:
  print("You win")
elif comp_random ==0 and get_input ==2:
  print("You lose")
elif comp_random > get_input:
  print("You lose")
elif get_input > comp_random:
  print("You win")
elif comp_random == get_input:
  print("Draw Game")
elif get_input>=3 or get_input<0:
  print("Invalid number, You lose")

Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)