3

I'm trying to make my 'hacking' portion of a Fallout terminal simulator more akin to what is actually in the game. I'm in the process of setting it up so that it uses a word list, picks some words from it, and then puts them into the list of address-like display. However, because the length of the words ranges from 4 letters (FISH) to 12 letters (Hippopotamus), the lines get staggered, and the pipes I'm using to separate the options no longer line up.

I tried using an answer I found here, which works fine for ust spaces but trying to insert

{"+varNameHere+":<15}".format(a)

in the middle of the string,

(typing_.typingPrint('0xCC01 |{)&@*'+"{:"+fillerChar+"^12}".format(words[0])+'@ |0xCC02 |!(#'+"{:"+fillerChar+"^12}".format(words[0])+'@)!#\n')

, throws an ValueError about there only being one '}' in the format string. I understand what the error is talking about, but the reason for my question is, Is there anyway to have the padding character be randomly decided when the string is printed out into the terminal window?

Code:

possibleChars = ['!','@','#','$','%','^','&','*','(',')',';',':']

fillerChar = random.sample(possibleChars)


print('0xCC01 |{)&@*'+"{:"+fillerChar+"^12}".format(words[0])+'@ |0xCC02 |!(#'+"{:"+fillerChar+"^12}".format(words[0])+'@)!#\n')
Jaxer5636
  • 97
  • 10
  • 5
    Please post the exact code you are using instead of trying to describe it. Please read [mre]. Ensure that it is [formatted properly](https://stackoverflow.com/editing-help). – MattDMo May 18 '23 at 22:50
  • This looks like a perfect opportunity **to not** use a format string, to be honest. – Maarten Bodewes May 18 '23 at 23:06

2 Answers2

2

Create a random 15-character string. Then extract a slice of it long enough to pad out what you want.

random_string = 'weopi94nf0683d0'
word = 'FISH'
left = (len(random_string) - len(word)) // 2
right = left + len(word)
padded_word = random_string[:left] + word + random_string[right:]
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Using what @Barmar put in his answer, I set up the script to replace whatever words were selected from the word list with their padded variant.

def setChars():
    for i in range(len(wordlist)):
        left = (len(random_string) - len(wordlist[i])) // 2
        right = left + len(wordlist[i])
        padded_word = random_string[:left] + wordlist[i] + random_string[right:]
        wordlist[i] = padded_word

I then added a line to replace the random_string from being hardcoded to being randomly generated with string.punctuation so that it would be different for each individual possible password choice.

def setChars():
    for i in range(len(wordlist)):
        random_string=''.join(ran.choices(string.punctuation, k=12))
        left = (len(random_string) - len(wordlist[i])) // 2
        right = left + len(wordlist[i])
        padded_word = random_string[:left] + wordlist[i] + random_string[right:]
        wordlist[i] = padded_word
Jaxer5636
  • 97
  • 10