Skip to content

Commit c8878ae

Browse files
authored
Merge pull request #175 from brundabharadwaj/feature/gaming-apps
Feature/gaming apps
2 parents e0d180c + 95ed18b commit c8878ae

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Quiz:
2+
def __init__(self, questions):
3+
self.questions = questions
4+
self.score = 0
5+
6+
def play(self):
7+
for question, options, answer in self.questions:
8+
print(question)
9+
for i, option in enumerate(options, 1):
10+
print(f"{i}. {option}")
11+
user_answer = int(input("Your choice (1/2/3/4): "))
12+
if options[user_answer-1] == answer:
13+
print("Correct!\n")
14+
self.score += 1
15+
else:
16+
print(f"Wrong! Correct answer is: {answer}\n")
17+
print(f"Your final score is: {self.score}/{len(self.questions)}")
18+
19+
if __name__ == "__main__":
20+
questions = [
21+
("What is the capital of France?", ["Berlin", "Madrid", "Paris", "Rome"], "Paris"),
22+
("Which is the smallest prime number?", ["0", "1", "2", "3"], "2"),
23+
("What language is this code written in?", ["Java", "Python", "C++", "JavaScript"], "Python")
24+
]
25+
26+
game = Quiz(questions)
27+
game.play()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import random
2+
3+
def number_guessing_game():
4+
# Generate a random number between 1 and 100
5+
random_number = random.randint(1, 100)
6+
attempts = 0
7+
guess = None
8+
9+
print("Welcome to the Number Guessing Game!")
10+
print("I have chosen a number between 1 and 100, inclusive. Can you guess it?")
11+
12+
while guess != random_number:
13+
try:
14+
# Get the user's guess
15+
guess = int(input("Enter your guess (between 1 and 100): "))
16+
17+
# Increment the attempt counter
18+
attempts += 1
19+
20+
# Check if the guess is correct
21+
if guess < random_number:
22+
print("Too low! Try again.")
23+
elif guess > random_number:
24+
print("Too high! Try again.")
25+
else:
26+
print(f"Congratulations! You've guessed the number in {attempts} attempts.")
27+
break
28+
except ValueError:
29+
print("Please enter a valid number between 1 and 100.")
30+
31+
# Ask if the player wants to play again
32+
play_again = input("Do you want to play again? (yes/no) ").lower()
33+
if play_again == "yes":
34+
number_guessing_game()
35+
36+
if __name__ == "__main__":
37+
number_guessing_game()

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ We appreciate the contributions from the following community members:
9393
- [Shubham Atkal](https://github.com/shubhamatkal)
9494
- [Harsimran Singh](https://github.com/Harsimran-19)
9595
- [Ezhill Ragesh](https://github.com/ezhillragesh)
96+
- [Brunda Bharadwaj](https://github.com/brundabharadwaj/)
9697
9798
---
9899

0 commit comments

Comments
 (0)