0% found this document useful (0 votes)
35 views4 pages

Using Ramdom

The document describes how to use the random module in Python to create two simple games - Rock Paper Scissors and Hangman. It shows importing the random module, defining functions to run the game logic which includes randomly selecting the computer's or hidden word's choice, getting the player's input, checking for wins/losses/ties, and allowing the player to play again if desired. Variables are used to track game state like attempts remaining and letters already guessed.

Uploaded by

vowan44398
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views4 pages

Using Ramdom

The document describes how to use the random module in Python to create two simple games - Rock Paper Scissors and Hangman. It shows importing the random module, defining functions to run the game logic which includes randomly selecting the computer's or hidden word's choice, getting the player's input, checking for wins/losses/ties, and allowing the player to play again if desired. Variables are used to track game state like attempts remaining and letters already guessed.

Uploaded by

vowan44398
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

USE of Ramdom Module

Rock, Papers, Scissors


# Python Random Module
import random

# Intro
print("Rock, Paper, Scissors...")

# Function
def try_again():
# Random Choice (Rock, Paper, or Scissors)
R_P_S = ["Rock", "Paper", "Scissors"]
computer = random.choice(R_P_S)

# Player's choice
player = input("your choice: ").lower().capitalize()

# If the program chose rock


if computer == "Rock":
# If the player chose rock
if player == "Rock":
print(f"I chose {computer}, you chose {player}\nit's a tie!")
# If the player chose paper
elif player == "Paper":
print(f"I chose {computer}, you chose {player}\nYou win!")
# If the player chose scissors
elif player == "Scissors":
print(f"I chose {computer}, you chose {player}\nI win!")

# If the program chose paper


elif computer == "Paper":
# If the player chose rock
if player == "Rock":
print(f"I chose {computer}, you chose {player}\nI win!")
# If the player chose paper
elif player == "Paper":
print(f"I chose {computer}, you chose {player}\nIt's a tie!")
# If the player chose scissors
elif player == "Scissors":
print(f"I chose {computer}, you chose {player}\nYou win!")

# If the program chose scissors


elif computer == "Scissors":
# If the player chose rock
if player == "Rock":
print(f"I chose {computer}, you chose {player}\nYou win!")
# If the player chose paper
elif player == "Paper":
print(f"I chose {computer}, you chose {player}\nI win!")
# If the player chose scissors
elif player == "Scissors":
print(f"I chose {computer}, you chose {player}\nIt's a tie")

# If the player wants to play again


play_again = input("Do you want to play again? yes or no: ").lower().capitalize()
# If the player says yes, go back to the function
if play_again == "Yes":
try_again()
# If the player says no, say goodbye
elif play_again == "No":
print("Goodbye")

# End of function
try_again()
Hangman
# Python Random Module
import random

# Intro
print("Welcome to Hangman! I will choose a word and you have to guess its letters. You only have
6 attempts.")

# Function
def try_again():
# Random chooser
words = ["ignore"]
word_choice = random.choice(words)

# Variables
attempts = 0
a = False
b = False
c = False
d = False
e = False
f = False
g = False
h = False
i = False
j = False
k = False
l = False
m = False
n = False
o = False
p = False
q = False
r = False
s = False
t = False
u = False
v = False
w = False
x = False
y = False
z = False

# If the program chose a word, print it out with missing letters. If the user gets the letters correct,
change its variable to True and print it out. Once all the letters are found, the player won
if word_choice == "ignore":
print(" ____ n o __ e")
guess = input("type the missing letter: ")
while attempts < 6:
if guess == "i":
i=True
if g == True and r == True:
print("i g n o r e")
win = input(f"you won, you took {attempts} attempt(s), Do you want to play again?
Yes or No: ").lower().capitalize()
if win == "Yes":
try_again()
break
elif win == "No":
print("Goodbye")
break
elif r == True:
print("i __ n o r e")
guess = input("\ntype the missing letter: ")
elif g == True:
print("i g n o __ e")
guess = input("\ntype the missing letter: ")
else:
print("i __n o__ e")
guess = input("\ntype the missing letter: ")
elif guess == "g":
g = True
if i == True and r == True:
print("i g n o r e")
win = input(f"you won, you took {attempts} attempt(s), Do you want to play again?
Yes or No: ").lower().capitalize()
if win == "Yes":
try_again()
break
elif win == "No":
print("Goodbye")
break
elif r == True:
print("__ g n o r e")
guess = input("\ntype the missing letter: ")
elif i == True:
print("i g n o __ e")
guess = input("\ntype the missing letter: ")
else:
print(" __g n o__ e")
guess = input("\ntype the missing letter: ")
elif guess == "r":
r = True
if i == True and g == True:
print("i g n o r e")
win = input(f"you won, you took {attempts} attempt(s), Do you want to play again?
Yes or No: ").lower().capitalize()
if win == "Yes":
try_again()
break
elif win == "No":
print("Goodbye")
break
elif g == True:
print("__ g n o r e")
guess = input("\ntype the missing letter: ")
elif i == True:
print("i __ n o r e")
else:
print(" ____ n o r e")
guess = input("\ntype the missing letter: ")
else:
print("Try Again")
attempts += 1
guess = input("\ntype the missing letter: ")

# If all of the player's attempts lost, game over


if not attempts < 6:
game_over = input("Game Over. Do you want to play again? Yes or No:
").lower().capitalize()
if game_over == "Yes":
try_again()
elif game_over == "No":
print("Goodbye")

You might also like