I am trying to recreate the automate the boring stuff with python. the problem is that I am having is that my functions depend on each other. I have tried to update the functions with the parameters but it seems like I am going in circles or playing whack a mole. fix one problem, another one appears.
The goal is to generate a function that is capable of generating a memory address that starts with a hex number and than have a function acitvate that would fill in a random number of garbage characters. %@#$ is what I mean by garbge characters. after that a game word would be placed. and finally the rest of the 16 character space would be filled with garbage characters.
My generator game_row_function is the culprit.
can anyone look at my code and give me pointers? chat gpt is not able to help.
https://paste.pythondiscord.com/7Y6A
automate the boring stuff with python example:
https://inventwithpython.com/bigbookpython/project33.html
# IMPORTS
import sys
import random
"""
https://inventwithpython.com/bigbookpython/project33.html
basically game_words is where I am telling python to grab the 15 words used fo the hacking mini game.
Players select from this list of words hoping to guess the password.
I need 3 words that have 0 letters in common with the password
and another 3 words that have 1,2,3,4 letters in common with the password respectivley and the password all stored in a way that python can know about it.
after that I need to make a game board that will display the characters and the words on the screen.
"""
# -----------------------------------------------------------------#
# CONSTANTS
# garbage characters system
garbage_chars = "~!@#$%^&*()_+-={}[]|;:,.<>?/"
def introduce():
name = input("To start the game please type in your player name: ")
print(
f"Agent {name}! The evil dictator Kim Jong Un has decided to launch the nukes. All of humanity's hopes rest on your shoulders to hack into the system and stop the launch. You will see a series of possible words that are the password. Use our hint system software to determine if you are close to guessing the password. The hint system will tell you the letters that the word choice and the password have in common as well as positioning. You are our last hope.... \n Okay agent {name}, here are list of the possible passwords. \n Type in the word from the available list of words that you believe is the password."
)
# This function generates a password for the player to guess.
def get_password(word_list):
password = random.choice(word_list)
print("test: This is a test for the password: ", password)
return password
# this function will gather in the game words from the sevenletterwords.txt file
def get_word_list():
with open("sevenletterwords.txt", "r") as file:
word_list = [line.strip().upper() for line in file.readlines()]
random.shuffle(word_list)
# test print(full_list_of_words)
return word_list
def hex_number():
number = random.randint(1000, 9999)
hex_number = hex(number)
return hex_number
# This function gets one game word from the game word list to place in the game board row.
# I want this function to randomly select one word from the game_word_set.
def get_game_word(game_word_set):
one_game_word = random.choice(game_word_set)
return one_game_word
# this function will fill the game row with garbage characters.
def garbage_character_filler(one_game_word):
garbage_row = []
garbage_placement = random.randint(2, 8)
# we will fill the game row with a random amount of garbage characters
for i in range(garbage_placement):
# print(random.choice(garbage_chars), end="")
garbage_row.append(random.choice(garbage_chars))
for ending_characters in range(9 - len(garbage_row)):
random.choice(garbage_chars),
garbage_row.append(ending_characters)
return str(garbage_row)
# this function is meant to combine the hex number, game word, and garbage characters togather.
def generate_game_row():
hex_str = hex_number()
garbage_str = garbage_character_filler()
game_word_str = get_game_word(game_word_set)
# Calculate the length of the final string
total_length = len(hex_str) + len(garbage_str) + len(game_word_str)
# Check if the total length is less than 16, and pad the string with garbage characters if needed
if total_length < 16:
remaining_length = 16 - total_length
garbage_padding = "".join(
random.choice(garbage_chars) for _ in range(remaining_length)
)
return hex_str + garbage_str + game_word_str + garbage_padding
else:
return hex_str + garbage_str + game_word_str
# Example usage
row = generate_game_row()
print(row)
# the game words need to have a certain amount of characters in common with the password.
def get_n_overlap(password, n):
overlapping_words = []
x = 0
for word in word_list:
if x < 3:
# if the number of matching letters is the same as n than append that word to the list.
overlap = set(password) & set(word)
if len(overlap) == n and word != password:
overlapping_words.append(word)
x += 1
if x == 3:
break
return overlapping_words
# DRIVER CODE ---------------------
# these lines of code are here to prevent not defined issues.
# we call these functions to grab the game words list and the password.
word_list = get_word_list()
password = get_password(word_list)
# we establish a place to hold the values
game_words_dictionary = {}
game_words = []
# we can target keys in a dictionary with variables!
# I want to use this technique once I get all three of them in there.
# place the words that have 0 letters in common into the dictionary at the 0 index.
game_words_dictionary[0] = get_n_overlap(password, 0)
# now do the same things for the other words
game_words_dictionary[1] = get_n_overlap(password, 1)
game_words_dictionary[2] = get_n_overlap(password, 2)
game_words_dictionary[3] = get_n_overlap(password, 3)
game_words_dictionary[4] = get_n_overlap(password, 4)
# now we combine these values in the dictionary togather into a single list.
game_word_set = sum(game_words_dictionary.values(), [])
game_word_set.append(password)
# all of the game words are shuffled and random.shuffle will shuffle the values in place.
# alright the game words are finally established!
random.shuffle(game_word_set)
# now I need to get the hex() number, garbage character function run, and than have the game word be put into place.
game_word = get_game_word(game_word_set)
row = generate_game_row(game_word)
def main():
rows = 16
columns = 2
tries = random.randint(3, 5)
# introduce()
# word_list = get_word_list()
# password = get_password(word_list)
# If this program was run (instead of imported), run the game:
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.exit() # When Ctrl-C is pressed, end the program.
# random will change things in place. there is no need for variables.