Skip to content

Commit f290961

Browse files
Merge pull request #2341 from andoriyaprashant/branch4
Coding language learning game added
2 parents 9c40bd0 + cebf3f1 commit f290961

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Coding Language Learning Game
2+
3+
This is a Python-based interactive game that helps users learn coding languages by solving programming challenges in a gamified environment. Players have to guess the output of Python code snippets, earn points for correct guesses, and progress through different difficulty levels.
4+
5+
## Features
6+
7+
- Multiple Difficulty Levels: Choose from easy, medium, and hard levels, each with corresponding sets of code snippets.
8+
- Time Limit: Each code snippet comes with a time limit. The player must guess the output within the specified time to earn points.
9+
- Lives System: Start with a certain number of lives and lose one for each incorrect guess. The game ends when all lives are lost.
10+
- Hints: Players can request hints for code snippets to receive some guidance.
11+
- Score Multipliers: Earn bonus points for consecutive correct guesses.
12+
13+
## Requirements
14+
15+
- Python 3.x
16+
17+
## How to Run
18+
19+
1. Clone this repository to your local machine.
20+
2. Install the required packages listed in `requirements.txt` using pip:
21+
3. Run the `coding_language_learning_game.py` script in your Python environment to start the game.
22+
4. Choose a difficulty level and start guessing the output of the given Python code snippets.
23+
24+
## How to Play
25+
26+
1. Select a difficulty level (easy, medium, hard) at the beginning of the game.
27+
2. A Python code snippet will be displayed, and you need to guess the output within the time limit.
28+
3. If your guess is correct, you earn 10 points.
29+
4. If your guess is incorrect or time runs out, you lose a life.
30+
5. The game continues until you run out of lives or decide to stop playing.
31+
6. Your final score will be displayed at the end of the game.
32+
33+
Enjoy the Coding Language Learning Game and have fun while improving your coding skills!
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import random
2+
import time
3+
4+
# Dictionary containing Python code snippets and their expected outputs
5+
code_snippets = {
6+
"print('Hello, world!')": "Hello, world!",
7+
"print(2 + 3)": "5",
8+
"print('Python' + ' is ' + 'fun')": "Python is fun",
9+
"numbers = [1, 2, 3, 4, 5]\nprint(numbers)": "[1, 2, 3, 4, 5]",
10+
"num1 = 10\nnum2 = 5\nprint(num1 * num2)": "50",
11+
"def fibonacci(n):\n if n <= 0:\n return 'Invalid input'\n elif n == 1:\n return 0\n elif n == 2:\n return 1\n else:\n return fibonacci(n-1) + fibonacci(n-2)\nprint(fibonacci(6))": "5",
12+
}
13+
14+
# Difficulty levels with corresponding sets of code snippets
15+
difficulty_levels = {
16+
"easy": ["print('Hello, world!')", "print(2 + 3)"],
17+
"medium": ["print('Python' + ' is ' + 'fun')", "numbers = [1, 2, 3, 4, 5]\nprint(numbers)"],
18+
"hard": ["num1 = 10\nnum2 = 5\nprint(num1 * num2)", "def fibonacci(n):\n if n <= 0:\n return 'Invalid input'\n elif n == 1:\n return 0\n elif n == 2:\n return 1\n else:\n return fibonacci(n-1) + fibonacci(n-2)\nprint(fibonacci(6))"]
19+
}
20+
21+
def get_random_code_snippet(difficulty):
22+
"""Return a random Python code snippet based on the selected difficulty."""
23+
return random.choice(difficulty_levels[difficulty])
24+
25+
def main():
26+
print("Welcome to the Coding Language Learning Game!")
27+
difficulty = input("Select difficulty (easy, medium, hard): ").lower()
28+
29+
# Initialize game settings
30+
lives = 3
31+
score = 0
32+
time_limit = 45 # Time limit in seconds for each code snippet
33+
34+
while True:
35+
code_snippet = get_random_code_snippet(difficulty)
36+
expected_output = code_snippets[code_snippet]
37+
38+
print("\nCode Snippet:")
39+
print(code_snippet)
40+
41+
start_time = time.time()
42+
user_guess = input("Your Guess: ")
43+
44+
elapsed_time = time.time() - start_time
45+
if elapsed_time > time_limit:
46+
print(f"Time's up! The correct output was: {expected_output}")
47+
lives -= 1
48+
elif user_guess == expected_output:
49+
print("Correct! You earned 10 points.")
50+
score += 10
51+
else:
52+
print(f"Oops! The correct output was: {expected_output}")
53+
lives -= 1
54+
55+
if lives == 0:
56+
print("Game Over! You've run out of lives.")
57+
break
58+
59+
print(f"Lives remaining: {lives}")
60+
print(f"Your current score: {score}")
61+
62+
play_again = input("Do you want to play again? (y/n): ")
63+
if play_again.lower() != "y":
64+
break
65+
66+
print(f"Your final score: {score}")
67+
68+
if __name__ == "__main__":
69+
main()

0 commit comments

Comments
 (0)