Created
March 11, 2022 08:33
-
-
Save ashutoshkrris/8cb234eb42143265b0795480abde6007 to your computer and use it in GitHub Desktop.
Sentence Rearrangement Game using Python
This file contains hidden or 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
from typing import List | |
from random import shuffle, choice | |
from rich.console import Console | |
console = Console() | |
def get_sentences(filename: str) -> List: | |
with open(filename) as f: | |
sentences = list(set(f.read().splitlines())) | |
return sentences | |
def get_jumbled(sentence: str) -> str: | |
splitted_words = sentence.split() | |
shuffle(splitted_words) | |
jumbled_sentence = " / ".join(splitted_words) | |
return jumbled_sentence | |
def game(number_of_questions: int) -> int: | |
game_is_continued = True | |
score = 0 | |
count = 0 | |
while game_is_continued: | |
correct_sentence = choice(get_sentences("sentences.txt")) | |
jumbled_sentence = get_jumbled(correct_sentence) | |
console.print(f"\nHere's your sentence: \n[cyan]{jumbled_sentence}[/cyan]") | |
user_sentence = input("\nWrite your answer answer below\n") | |
if user_sentence.lower() == correct_sentence.lower(): | |
score += 1 | |
console.print('[green]Hooray! Correct Answer[/green]') | |
else: | |
console.print('[red]Oops! Incorrect Answer[/red]') | |
console.print(f'Correct Answer : [blue]{correct_sentence}[/blue]') | |
count += 1 | |
if count == number_of_questions: | |
game_is_continued = False | |
return score | |
if __name__ == "__main__": | |
console.print('[magenta]Welcome to the GAME!![/magenta]\n') | |
number_of_questions = int(input("Please enter number of questions : ")) | |
if number_of_questions > 0: | |
final_score = game(number_of_questions) | |
console.print(f'\n[green]Your final score is {final_score}[/green]') | |
print("Adiós, Amigo") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment