diff --git a/Hangman game.py b/Hangman game.py new file mode 100644 index 0000000..fa978cc --- /dev/null +++ b/Hangman game.py @@ -0,0 +1,79 @@ +from collections import Counter + +someWords = 'apple banana mango' + +someWords = someWords.split(' ') +# randomly choose a secret word from our "someWords" LIST. +word = random.choice(someWords) + +if __name__ == '__main__': + print('Guess the word! HINT: word is a name of a fruit') + + for i in word: + # For printing the empty spaces for letters of the word + print('_', end=' ') + print() + + playing = True + # list for storing the letters guessed by the player + letterGuessed = '' + chances = len(word) + 2 + correct = 0 + flag = 0 + try: + while (chances != 0) and flag == 0: # flag is updated when the word is correctly guessed + print() + chances -= 1 + + try: + guess = str(input('Enter a letter to guess: ')) + except: + print('Enter only a letter!') + continue + + # Validation of the guess + if not guess.isalpha(): + print('Enter only a LETTER') + continue + elif len(guess) > 1: + print('Enter only a SINGLE letter') + continue + elif guess in letterGuessed: + print('You have already guessed that letter') + continue + + # If letter is guessed correctly + if guess in word: + # k stores the number of times the guessed letter occurs in the word + k = word.count(guess) + for _ in range(k): + letterGuessed += guess # The guess letter is added as many times as it occurs + + # Print the word + for char in word: + if char in letterGuessed and (Counter(letterGuessed) != Counter(word)): + print(char, end=' ') + correct += 1 + # If user has guessed all the letters + # Once the correct word is guessed fully, + elif (Counter(letterGuessed) == Counter(word)): + # the game ends, even if chances remain + print("The word is: ", end=' ') + print(word) + flag = 1 + print('Congratulations, You won!') + break # To break out of the for loop + break # To break out of the while loop + else: + print('_', end=' ') + + # If user has used all of his chances + if chances <= 0 and (Counter(letterGuessed) != Counter(word)): + print() + print('You lost! Try again..') + print('The word was {}'.format(word)) + + except KeyboardInterrupt: + print() + print('Bye! Try again.') + exit() diff --git a/Morsecode generator.py b/Morsecode generator.py new file mode 100644 index 0000000..2593b4a --- /dev/null +++ b/Morsecode generator.py @@ -0,0 +1,97 @@ +MORSE_CODE_DICT = {'A': '.-', 'B': '-...', + 'C': '-.-.', 'D': '-..', 'E': '.', + 'F': '..-.', 'G': '--.', 'H': '....', + 'I': '..', 'J': '.---', 'K': '-.-', + 'L': '.-..', 'M': '--', 'N': '-.', + 'O': '---', 'P': '.--.', 'Q': '--.-', + 'R': '.-.', 'S': '...', 'T': '-', + 'U': '..-', 'V': '...-', 'W': '.--', + 'X': '-..-', 'Y': '-.--', 'Z': '--..', + '1': '.----', '2': '..---', '3': '...--', + '4': '....-', '5': '.....', '6': '-....', + '7': '--...', '8': '---..', '9': '----.', + '0': '-----', ', ': '--..--', '.': '.-.-.-', + '?': '..--..', '/': '-..-.', '-': '-....-', + '(': '-.--.', ')': '-.--.-'} + +# Function to encrypt the string +# according to the morse code chart + + +def encrypt(message): + cipher = '' + for letter in message: + if letter != ' ': + + # Looks up the dictionary and adds the + # corresponding morse code + # along with a space to separate + # morse codes for different characters + cipher += MORSE_CODE_DICT[letter] + ' ' + else: + # 1 space indicates different characters + # and 2 indicates different words + cipher += ' ' + + return cipher + +# Function to decrypt the string +# from morse to english + + +def decrypt(message): + + # extra space added at the end to access the + # last morse code + message += ' ' + + decipher = '' + citext = '' + for letter in message: + + # checks for space + if (letter != ' '): + + # counter to keep track of space + i = 0 + + # storing morse code of a single character + citext += letter + + # in case of space + else: + # if i = 1 that indicates a new character + i += 1 + + # if i = 2 that indicates a new word + if i == 2: + + # adding space to separate words + decipher += ' ' + else: + + # accessing the keys using their values (reverse of encryption) + decipher += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT + .values()).index(citext)] + citext = '' + + return decipher + +# Hard-coded driver function to run the program + + +def main(): + message = input("type the message : ") + result = encrypt(message.upper()) + print(result) + + +''' + message = "--. . . -.- ... -....- ..-. --- .-. -....- --. . . -.- ... " + result = decrypt(message) + print(result) +''' + +# Executes the main function +if __name__ == '__main__': + main() diff --git a/flames.py b/flames.py new file mode 100644 index 0000000..ef782a6 --- /dev/null +++ b/flames.py @@ -0,0 +1,195 @@ +# import all functions from the tkinter +from tkinter import * + +# function for removing common characters +# with their respective occurrences + + +def remove_match_char(list1, list2): + + for i in range(len(list1)): + for j in range(len(list2)): + + # if common character is found + # then remove that character + # and return list of concatenated + # list with True Flag + if list1[i] == list2[j]: + c = list1[i] + + # remove character from the list + list1.remove(c) + list2.remove(c) + + # concatenation of two list elements with * + # * is act as border mark here + list3 = list1 + ["*"] + list2 + + # return the concatenated list with True flag + return [list3, True] + + # no common characters is found + # return the concatenated list with False flag + list3 = list1 + ["*"] + list2 + return [list3, False] + + +# function for telling the relationship status +def tell_status(): + + # take a 1st player name from Player1_field entry box + p1 = Player1_field.get() + + # converted all letters into lower case + p1 = p1.lower() + + # replace any space with empty string + p1.replace(" ", "") + + # make a list of letters or characters + p1_list = list(p1) + + # take a 2nd player name from Player2_field entry box + p2 = Player2_field.get() + p2 = p2.lower() + p2.replace(" ", "") + p2_list = list(p2) + + # taking a flag as True initially + proceed = True + + # keep calling remove_match_char function + # until common characters is found or + # keep looping until proceed flag is True + while proceed: + + # function calling and store return value + ret_list = remove_match_char(p1_list, p2_list) + + # take out concatenated list from return list + con_list = ret_list[0] + + # take out flag value from return list + proceed = ret_list[1] + + # find the index of "*" / border mark + star_index = con_list.index("*") + + # list slicing perform + + # all characters before * store in p1_list + p1_list = con_list[: star_index] + + # all characters after * store in p2_list + p2_list = con_list[star_index + 1:] + + # count total remaining characters + count = len(p1_list) + len(p2_list) + + # list of FLAMES acronym + result = ["Friends", "Love", "Affection", "Marriage", "Enemy", "Siblings"] + + # keep looping until only one item + # is not remaining in the result list + while len(result) > 1: + + # store that index value from + # where we have to perform slicing. + split_index = (count % len(result) - 1) + + # this steps is done for performing + # anticlock-wise circular fashion counting. + if split_index >= 0: + + # list slicing + right = result[split_index + 1:] + left = result[: split_index] + + # list concatenation + result = right + left + + else: + result = result[: len(result) - 1] + + # insert method inserting the + # value in the text entry box. + Status_field.insert(10, result[0]) + + +# Function for clearing the +# contents of all text entry boxes +def clear_all(): + Player1_field.delete(0, END) + Player2_field.delete(0, END) + Status_field.delete(0, END) + + # set focus on the Player1_field entry box + Player1_field.focus_set() + + +# Driver code +if __name__ == "__main__": + + # Create a GUI window + root = Tk() + + # Set the background colour of GUI window + root.configure(background='light green') + + # Set the configuration of GUI window + root.geometry("350x125") + + # set the name of tkinter GUI window + root.title("Flames Game") + + # Create a Player 1 Name: label + label1 = Label(root, text="Player 1 Name: ", + fg='black', bg='dark green') + + # Create a Player 2 Name: label + label2 = Label(root, text="Player 2 Name: ", + fg='black', bg='dark green') + + # Create a Relation Status: label + label3 = Label(root, text="Relationship Status: ", + fg='black', bg='red') + + # grid method is used for placing + # the widgets at respective positions + # in table like structure . + label1.grid(row=1, column=0, sticky="E") + label2.grid(row=2, column=0, sticky="E") + label3.grid(row=4, column=0, sticky="E") + + # Create a text entry box + # for filling or typing the information. + Player1_field = Entry(root) + Player2_field = Entry(root) + Status_field = Entry(root) + + # grid method is used for placing + # the widgets at respective positions + # in table like structure . + # ipadx keyword argument set width of entry space . + Player1_field.grid(row=1, column=1, ipadx="50") + Player2_field.grid(row=2, column=1, ipadx="50") + Status_field.grid(row=4, column=1, ipadx="50") + + # Create a Submit Button and attached + # to tell_status function + button1 = Button(root, text="Submit", bg="red", + fg="black", command=tell_status) + + # Create a Clear Button and attached + # to clear_all function + button2 = Button(root, text="Clear", bg="red", + fg="black", command=clear_all) + + # grid method is used for placing + # the widgets at respective positions + # in table like structure . + button1.grid(row=3, column=1) + button2.grid(row=5, column=1) + + # Start the GUI + root.mainloop()