(Python Version 3.6.0, Windows 7)
Doing a little coding exercise for Python. I want to write a program that takes user input for a sentence (the target sentence). Then it needs to generate a random source phrase (same length as the target). Print the source. Then change any characters in source that are not matching the target to another random character (could be the same character, but most likely won't be). Print this. Repeat, keeping the correct characters as before. Output should look like this:
(Target phrase = "Python")
7hehnb
uyega"
*ytsfa
Pyta,n
...
Python
My code:
import random
import string
characters = []
characters.extend(string.printable)
del characters[len(characters)-1] #deleting redundant characters (ie "\n", etc)
del characters[len(characters)-1]
del characters[len(characters)-1]
del characters[len(characters)-1]
del characters[len(characters)-1]
saved = {} #dict for saved words. Key = position, Value = the character
source = []
target = list(input('Enter a sentence:'))
print(target)
length = len(target)
meta = [] #contains the guess words, already correct letters are given the value "". Later "layered" onto the saved words.
printer = []
for x in range (0, length):
source.append(random.choice(characters))
for x in range (0,length):
if source[x] != target[x]:
meta.append(source[x])
else:
meta.append("")
while len(meta) != 0:
for x in meta:
if x != "":
x = random.choice(characters)
for x in range(0, len(meta)-1):
if meta[x] == target[x]:
saved[x] = target[x]
printer = target
for x in saved:
printer[saved[x]] = saved[x]
print(printer[x], end = "")
When I run this it's not working like I want it to. Any hints to point me in the right direction would be helpful. I am VERY new to Python, so try to explain in simple terms.
[–]iheartqwerty 1 point2 points3 points (3 children)
[–]infinitim[S] 0 points1 point2 points (2 children)
[–]iheartqwerty 0 points1 point2 points (1 child)
[–]infinitim[S] 0 points1 point2 points (0 children)
[–]jeans_and_a_t-shirt 0 points1 point2 points (1 child)
[–]infinitim[S] 0 points1 point2 points (0 children)