Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Java and Python Major Project 2

This Python code defines functions for a treasure hunt game that generates a random treasure position and includes scenarios for a snake room and monster room. The main game loop prompts the player to choose a left or right door, then calls the appropriate room function. It then prompts the player to guess the treasure position, tracking attempts, and plays again if desired.

Uploaded by

shashank kulal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Java and Python Major Project 2

This Python code defines functions for a treasure hunt game that generates a random treasure position and includes scenarios for a snake room and monster room. The main game loop prompts the player to choose a left or right door, then calls the appropriate room function. It then prompts the player to guess the treasure position, tracking attempts, and plays again if desired.

Uploaded by

shashank kulal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java and Python: Major Project

Project Title: Treasure Hunt Game using Python 3


CODE:
import random

# Function to generate a random position for the treasure


def generate_treasure_position():
return random.randint(1, 20)

# Function for the snake room scenario


def snake_room():
print("You entered the snake room!")
print("There is a giant snake in front of you.")
print("You have two options:")
print("1. Try to tame the snake")
print("2. Flee from the room")

choice = int(input("Enter your choice (1 or 2): "))

if choice == 1:
print("You tried to tame the snake, but it bit you.")
print("Game over!")
lets_play_again()
elif choice == 2:
print("You fled from the snake room and continue your adventure.")

# Function for the monster room scenario


def monster_room():
print("You entered the monster room!")
print("There is a fierce monster blocking your path.")
print("You have two options:")
print("1. Sneak past the monster")
print("2. Fight the monster ")

choice = int(input("Enter your choice (1 or 2): "))

if choice == 2:
print("You fought bravely against the monster, but it overpowered
you.")
print("Game over!")
lets_play_again()
elif choice == 1:
print("You managed to sneak past the monster and continue your
adventure.")

# Function to ask the player if they want to play again


def lets_play_again():
play_again = input("Do you want to play again? (yes or no): ")

if play_again.lower() == "yes":
start_game()
else:
print("Thank you for playing the Treasure Hunt Game!")

# Main game loop


def start_game():
print("Welcome to the Treasure Hunt Game!")
print("You are standing in a dark room.")
print("There is a door to your left and right.")

choice = input("Which one do you take? (l or r): ")

if choice == "l":
snake_room()
elif choice == "r":
monster_room()
else:
print("Invalid choice! Please enter 'l' or 'r'.")

treasure_position = generate_treasure_position()
attempts = 0

while True:
guess = int(input("Enter your guess (1-20): "))
attempts += 1

if guess < 1 or guess > 20:


print("Invalid input! Please enter a number between 1 and 20.")
continue

if guess < treasure_position:


print("The treasure is in a higher position.")
elif guess > treasure_position:
print("The treasure is in a lower position.")
else:
print("Congratulations! You found the treasure in", attempts,
"attempts.")
break
# Start the game
start_game()
OUTPUT:

You might also like