-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #327 from Chin-may02/main
Addition of (rock, paper scissor) and (Trivia)
- Loading branch information
Showing
4 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import random | ||
import math | ||
|
||
def play(): | ||
user = input("What's your choice? 'r' for rock, 'p' for paper, 's' for scissors\n") | ||
user = user.lower() | ||
|
||
computer = random.choice(['r', 'p', 's']) | ||
|
||
if user == computer: | ||
return (0, user, computer) | ||
|
||
|
||
if is_win(user, computer): | ||
return (1, user, computer) | ||
|
||
return (-1, user, computer) | ||
|
||
def is_win(player, opponent): | ||
|
||
if (player == 'r' and opponent == 's') or (player == 's' and opponent == 'p') or (player == 'p' and opponent == 'r'): | ||
return True | ||
return False | ||
|
||
def play_best_of(n): | ||
player_wins = 0 | ||
computer_wins = 0 | ||
wins_necessary = math.ceil(n/2) | ||
while player_wins < wins_necessary and computer_wins < wins_necessary: | ||
result, user, computer = play() | ||
if result == 0: | ||
print('It is a tie. You and the computer have both chosen {}. \n'.format(user)) | ||
elif result == 1: | ||
player_wins += 1 | ||
print('You chose {} and the computer chose {}. You won!\n'.format(user, computer)) | ||
else: | ||
computer_wins += 1 | ||
print('You chose {} and the computer chose {}. You lost :(\n'.format(user, computer)) | ||
|
||
if player_wins > computer_wins: | ||
print('You have won the best of {} games! What a champ :D'.format(n)) | ||
else: | ||
print('Unfortunately, the computer has won the best of {} games. Better luck next time!'.format(n)) | ||
|
||
|
||
if __name__ == '__main__': | ||
play_best_of(3) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Rock-Paper-Scissors Game | ||
|
||
🎯 Goal | ||
The goal of this project is to create an engaging Rock-Paper-Scissors game that allows players to compete against a computer opponent in a fun and interactive way. | ||
|
||
🧾 Description | ||
This project is a Rock-Paper-Scissors game where players choose their move (rock, paper, or scissors) and compete against a randomly selected move from the computer. The game determines the winner based on standard rules and provides immediate feedback on each round. It tracks the score across multiple rounds, creating a competitive experience. By implementing fundamental programming concepts in Python, it offers an entertaining way for users to engage with a classic game. | ||
|
||
🧮 What I had done! | ||
|
||
Created a command-line interface for users to input their choice of rock, paper, or scissors. | ||
The game consists of multiple rounds where players can play against the computer, receiving immediate feedback on wins, losses, or ties. | ||
At the end of the series of rounds, players receive their total score, and the game summarizes the results. | ||
📚 Libraries Needed | ||
|
||
random - To randomly select the computer's choice from the available options. | ||
📢 Conclusion | ||
In conclusion, this Rock-Paper-Scissors project demonstrates essential programming concepts such as random selection, user input handling, loops, and scoring in Python. It offers an engaging experience that encourages users to have fun while testing their luck and strategy. The code is straightforward yet effective, making it an excellent learning project for beginner developers aiming to enhance their understanding of basic programming logic. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Trivia Game | ||
🎯 Goal | ||
The goal of this project is to create an engaging trivia game that tests users' knowledge across various categories such as Geography, Mathematics, Literature, and Science. | ||
|
||
🧾 Description | ||
This project is a Trivia Game where players answer questions from selected categories. The game randomly selects questions, provides immediate feedback on answers, and tracks the player's score. It implements fundamental programming concepts in Python, providing an interactive experience that challenges users' knowledge. | ||
|
||
🧮 What I had done! | ||
Created a command-line interface for users to select categories and answer trivia questions. | ||
The game consists of multiple rounds where players can guess answers, receiving immediate feedback on correctness. | ||
At the end of each round, players receive their score, and the game summarizes their total score after all rounds. | ||
📚 Libraries Needed | ||
random - To randomly select questions from the trivia dataset. | ||
📢 Conclusion | ||
In conclusion, this Trivia Game project demonstrates essential programming concepts such as random selection, user input handling, loops, and scoring in Python. It offers an engaging experience that encourages users to test and expand their knowledge across various subjects. The code is straightforward yet effective, making it an excellent learning project for beginner developers aiming to enhance their understanding of basic programming logic. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import random | ||
|
||
def get_questions(): | ||
return { | ||
"Geography": { | ||
"What is the capital of France?": "Paris", | ||
"Which river is the longest in the world?": "Nile", | ||
"What is the largest desert in the world?": "Sahara", | ||
"Which country has the most natural lakes?": "Canada", | ||
"What mountain range separates Europe and Asia?": "Ural", | ||
"What is the smallest country in the world?": "Vatican City", | ||
"Which city is known as the Big Apple?": "New York", | ||
"What continent is Egypt located in?": "Africa", | ||
}, | ||
"Mathematics": { | ||
"What is 2 + 2?": "4", | ||
"What is the square root of 16?": "4", | ||
"What is the value of pi (up to two decimal points)?": "3.14", | ||
"What is 5 x 6?": "30", | ||
"What is the derivative of x^2?": "2x", | ||
"What is the Fibonacci sequence's first number?": "0", | ||
"What is 10% of 200?": "20", | ||
"What is the sum of the interior angles of a triangle?": "180", | ||
}, | ||
"Literature": { | ||
"Who wrote 'Romeo and Juliet'?": "Shakespeare", | ||
"What is the title of the first Harry Potter book?": "Harry Potter and the Philosopher's Stone", | ||
"Who wrote '1984'?": "George Orwell", | ||
"Which novel begins with 'Call me Ishmael'?": "Moby Dick", | ||
"Who wrote 'Pride and Prejudice'?": "Jane Austen", | ||
"What is the pen name of Samuel Clemens?": "Mark Twain", | ||
"Who wrote 'The Great Gatsby'?": "F. Scott Fitzgerald", | ||
"What is the first book in the 'Lord of the Rings' trilogy?": "The Fellowship of the Ring", | ||
}, | ||
"Science": { | ||
"What is the chemical symbol for gold?": "Au", | ||
"What planet is known as the Red Planet?": "Mars", | ||
"What gas do plants absorb from the atmosphere?": "Carbon dioxide", | ||
"What is the speed of light?": "299792458 m/s", | ||
"What is H2O commonly known as?": "Water", | ||
"What part of the cell contains the genetic material?": "Nucleus", | ||
"What is the powerhouse of the cell?": "Mitochondria", | ||
"What element does 'O' represent on the periodic table?": "Oxygen", | ||
}, | ||
} | ||
|
||
def trivia_game(): | ||
questions = get_questions() | ||
total_score = 0 | ||
rounds = 3 | ||
|
||
print("Welcome to the Trivia Game!") | ||
print("Categories: Geography, Mathematics, Literature, Science") | ||
|
||
for _ in range(rounds): | ||
category = input("\nChoose a category: ").strip().title() | ||
|
||
if category not in questions: | ||
print("Invalid category. Please try again.") | ||
continue | ||
|
||
question_items = list(questions[category].items()) | ||
random.shuffle(question_items) | ||
|
||
score = 0 | ||
total_questions = len(question_items) | ||
|
||
print(f"\nYou will be asked {total_questions} questions from the {category} category. Let's get started!") | ||
|
||
for question, answer in question_items: | ||
user_answer = input(f"{question} ") | ||
if user_answer.strip().lower() == answer.lower(): | ||
print("Correct!") | ||
score += 1 | ||
else: | ||
print(f"Wrong! The correct answer is {answer}.") | ||
|
||
print(f"Round over! Your score for this round: {score}/{total_questions}") | ||
total_score += score | ||
|
||
print(f"\nGame over! Your total score: {total_score}/{rounds * len(questions[category])}") | ||
|
||
if __name__ == "__main__": | ||
trivia_game() |