Using Gemini Chat on Collab

Random Number Generation, List Manipulation & Rock-Paper-Scissors Game Implementations #PurePythonSeries — Episode #16

J3
Jungletronics
7 min readJul 17, 2024

--

Supercharge your Programming in Colab with AI-Powered tools: Gemini Chat.

This notebook covers various aspects of Python programming, including:

1. Random Number Generation:
— Generating random integers and floats using the ‘random’ module.
— Randomly selecting elements from lists.

2. List Manipulation:
— Counting occurrences of elements in lists.
— Appending elements to lists.
— Finding the index of elements.
— Reversing and sorting lists.
— Removing elements from lists using ‘pop’.

3. Rock-Paper-Scissors Game Implementations:
— Multiple versions of the game with increasing complexity.
— Using ASCII art to represent choices.
— Handling user input and determining the winner.

In the end, we will experience Gemini Chat. Hold on tight!
Let’s get Started:

00#step — An integer random number between 0 and 10

import random
print(random.randint(1,10))
7

01#step — Generate a float random number between 0.0 and 1.0

print(random.random())
0.19203798450438125

02# step — Generate a float random number between 0.0 and 5.0

print(5*random.random())
3.7332815531953187

03#step — List: An example that uses most of the list methods

fruits = [‘orange’, ‘apple’, ‘pear’, ‘banana’, ‘kiwi’, ‘apple’, ‘banana’]
apples = fruits.count(‘apple’)
print(f’There are {apples} Apples on fruits list.’)
tangirines = fruits.count(‘tangerine’)
print(f’There are {tangirines} tangirines on fruits list.’)
print(fruits)
print(‘Adding tangirine’)
fruits.append(‘tangirine’)
tangirines = fruits.count(‘tangirine’)
print(f’There are {tangirines} tangirines on fruits list.’)
print(fruits)
banana_index = fruits.index(‘banana’)
print(f’Banana apppears on index {banana_index} on fruits list.’)
fruits.index(‘banana’, 0) # Find next banana starting at position 4
print(‘Reversing the list…’)
fruits.reverse()
print(fruits)
print(‘Adding grape’)
fruits.append(‘grape’)
print(fruits)
print(‘Sorting list…’)
fruits.sort()
print(fruits)
print(‘Poping one fruit…’)
fruit = fruits.pop()
print(fruits)
print(f’{fruit} was popped!’)

There are 2 Apples on fruits list.
There are 0 tangirines on fruits list.
['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
Adding tangirine
There are 1 tangirines on fruits list.
['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana', 'tangirine']
Banana apppears on index 3 on fruits list.
Reversing the list...
['tangirine', 'banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
Adding grape
['tangirine', 'banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
Sorting list...
['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear', 'tangirine']
Poping one fruit...
['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
tangirine was popped!

04#step — Choosing one name randomly

import random

names = ['j3', 'luk', 'peter', 'rachel']
random_name = random.choice(names)
print(random_name)
peter

05#step — RPS Game implementations V1

# rock = 0
# paper = 1
# scissors = 2

import random

list_game = ['rock', 'paper', 'scissors']
your_choice = int(input("Choose 0 for rock, 1 for paper, or 2 for scissors. "))
while your_choice not in range(0,3):
print(f'You chose {your_choice}. List index out of range. Please choose 0, 1 or 2!')
your_choice = int(input("Choose 0 for rock, 1 for paper, or 2 for scissors. "))
else:
print(f'You chose: {list_game[your_choice]}')
computer_choice = random.randint(0, len(list_game)-1 )
print(f'Computer chose: {list_game[computer_choice]}')
if (computer_choice == 0 and your_choice == 0):
print('There is a draw!')
elif (computer_choice == 0 and your_choice == 1):
print('You loose!')
elif (computer_choice == 0 and your_choice == 2):
print('You lose!')
elif (computer_choice == 1 and your_choice == 0):
print('You win!')
elif (computer_choice == 1 and your_choice == 1):
print('There is a draw!')
elif (computer_choice == 1 and your_choice == 2):
print('You win!')
elif (computer_choice == 2 and your_choice == 0):
print('You win!')
elif (computer_choice == 2 and your_choice == 1):
print('You loose!')
elif (computer_choice == 2 and your_choice == 2):
print('There is a draw!')
Choose 0 for rock, 1 for paper, or 2 for scissors. 3
You chose 3. List index out of range. Please choose 0, 1 or 2!
Choose 0 for rock, 1 for paper, or 2 for scissors. 2
You chose: scissors
Computer chose: scissors
There is a draw!

06#step — RPS Game implementations V2

import random

list_game = ['rock', 'paper', 'scissors']
your_choice = int(input("Choose 0 for rock, 1 for paper, or 2 for scissors: "))
computer_choice = random.randint(0, len(list_game) - 1)
print(f"Computer chose: {list_game[computer_choice]}")
print(f"You chose: {list_game[your_choice]}")
if computer_choice == your_choice:
print('There is a draw!')
elif (computer_choice == 0 and your_choice == 1) or (computer_choice == 1 and your_choice == 2) or (computer_choice == 2 and your_choice == 0):
print('You win!')
else:
print('You lose!')
Choose 0 for rock, 1 for paper, or 2 for scissors: 1
Computer chose: paper
You chose: paper
There is a draw!

07#step — Rock Paper Scissors ASCII Art

# Rock Paper Scissors ASCII Art

# Rock
print("""
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
""")

# Paper
print("""
_______
---' ____)____
______)
_______)
_______)
---.__________)
""")

# Scissors
print("""
_______
---' ____)____
______)
__________)
(____)
---.__(___)
""")

08#step — RPS Game implementations V3

# Game implementations V3
# Rock Paper Scissors ASCII Art
rock = """
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
"""

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

scissors = """
_______
---' ____)____
______)
__________)
(____)
---.__(___)
"""
imgs = [rock, paper, scissors]

import random

list_game = ['rock', 'paper', 'scissors']
your_choice = int(input("Choose 0 for rock, 1 for paper, or 2 for scissors: "))
computer_choice = random.randint(0, len(list_game) - 1)

print(f"Computer chose: {imgs[computer_choice]}")
print(f"You chose: {imgs[your_choice]}")

if computer_choice == your_choice:
print('There is a draw!')
elif (computer_choice == 0 and your_choice == 1) or (computer_choice == 1 and your_choice == 2) or (computer_choice == 2 and your_choice == 0):
print('You win!')
else:
print('You lose!')
Choose 0 for rock, 1 for paper, or 2 for scissors: 0
Computer chose:
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)

You chose:
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)

There is a draw!

09#step — RPS Game implementations V4
# Rock Paper Scissors ASCII Art

# Game implementations V4
# Rock Paper Scissors ASCII Art
rock = """
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
"""

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

scissors = """
_______
---' ____)____
______)
__________)
(____)
---.__(___)
"""
imgs = [rock, paper, scissors]

import random

list_game = ['rock', 'paper', 'scissors']
your_choice = int(input("Choose 0 for rock, 1 for paper, or 2 for scissors: "))
computer_choice = random.randint(0, len(list_game) - 1)

print(f"Computer chose:\n{imgs[computer_choice]}")
print(f"You chose:\n{imgs[your_choice]}")

if computer_choice == your_choice:
print("There is a draw!")
elif (computer_choice == 0 and your_choice == 1) or \
(computer_choice == 1 and your_choice == 2) or \
(computer_choice == 2 and your_choice == 0):
print("You win!")
else:
print("You lose!")
Choose 0 for rock, 1 for paper, or 2 for scissors: 2
Computer chose:

_______
---' ____)
(_____)
(_____)
(____)
---.__(___)

You chose:

_______
---' ____)____
______)
__________)
(____)
---.__(___)

You lose!

10#step — create rock paper scissors game in the console Using Gemini Chat on Google Collab. Click Generator and make a question…

# prompt: create rock paper scissors game in the console

import random

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

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

scissors = """
_______
---' ____)____
______)
__________)
(____)
---.__(___)
"""

# List of choices
choices = [rock, paper, scissors]

# Get user choice
user_choice = int(input("What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors.\n"))
while user_choice >= 3 or user_choice < 0:
print("You typed an invalid number, you lose!")
user_choice = int(input("What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors.\n"))

print(choices[user_choice])

# Computer's choice
computer_choice = random.randint(0, 2)
print("Computer chose:")
print(choices[computer_choice])

# Determine the winner
if user_choice == computer_choice:
print("It's a draw")
elif (user_choice == 0 and computer_choice == 2) or (user_choice == 1 and computer_choice == 0) or (user_choice == 2 and computer_choice == 1):
print("You win!")
else:
print("You lose")
What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors.
0

_______
---' ____)
(_____)
(_____)
(____)
---.__(___)

Computer chose:

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

You lose

That’s all folks!

👉Project — Pure Python Episodes List (Project #16)

Supercharge your Programming in Colab with AI-Powered tools — Subscribe to the Google Research Channel → https://goo.gle/GoogleResearch
What is Colab?

Colab notebooks allow you to combine executable code
and rich text in a single document, along with images,
HTML, LaTeX and more. When you create your own
Colab notebooks, they are stored in your
Google Drive account. You can easily share your
Colab notebooks with co-workers or friends,
allowing them to comment on your notebooks or even edit them.

Related Posts

00#Episode#PurePythonSeries — Lambda in Python — Python Lambda Desmistification

01#Episode#PurePythonSeries — Send Email in Python — Using Jupyter Notebook — How To Send Gmail In Python

02#Episode#PurePythonSeries — Automate Your Email With Python & Outlook — How To Create An Email Trigger System in Python

03#Episode#PurePythonSeries — Manipulating Files With Python — Manage Your Lovely Photos With Python!

04#Episode#PurePythonSeries — Pandas DataFrame Advanced — A Complete Notebook Review

05#Episode#PurePythonSeries — Is This Leap Year? Python Calendar — How To Calculate If The Year Is Leap Year and How Many Days Are In The Month

06#Episode#PurePythonSeries — List Comprehension In Python — Locked-in Secrets About List Comprehension

07#Episode#PurePythonSeries — Graphs — In Python — Extremely Simple Algorithms in Python

08#Episode#PurePythonSeries — Decorator in Python — How To Simplifying Your Code And Boost Your Function

12#Episode#PurePythonSeries — Advanced Python Technologies qrcode, Speech Recognition in Python, Google Speech Recognition

13#Episode#PurePythonSeries — Advanced Python Technologies II — qFace Recognition w/ Jupyter Notebook & Ubuntu

14#Episode#PurePythonSeries — Advanced Python Technologies III — Face Recognition w/ Colab

15#Episode#PurePythonSeries — ISS Tracking Project — Get an Email alert when International Space Station (ISS) is above of us in the sky, at night

16#Episode#PurePythonSeries —Using Gemini Chat on Collab —Random Number Generation, List Manipulation & Rock-Paper-Scissors Game Implementations (this one)

--

--

J3
Jungletronics

Hi, Guys o/ I am J3! I am just a hobby-dev, playing around with Python, Django, Ruby, Rails, Lego, Arduino, Raspy, PIC, AI… Welcome! Join us!