The Student Room Group

Help with code for Guess the Word game

I use python and I am almost done with a guess the word game. I need help with getting a letter to replace a dash if the user guesses correctly. Then I have to transfer this to the canvas for GUI. Thank you. I appreciate the help
On the letter replacement, it sounds like you're trying to work with two different strings which are both the same length, but having different content (one with dashes and one with the word?)

Being nit-picky, python doesn't "technically" allow you to replace a character in a string, but that's no bother because you can use a list-of-characters and turn that into a string instead: https://stackoverflow.com/questions/4481724/convert-a-list-of-characters-into-a-string
fred = ['a', 'b', 'c', 'd']
barney = ''.join(fred)
print(barney)


Just for completeness, here's how to do the opposite of converting a string-to-list using list():
wilma = "python"
betty = list(wilma)
print(betty)


Armed with that knowledge, you could create a list of masked characters, only including those letters which your user has already guessed for example: https://repl.it/repls/LameOurCybernetics
word = "python"

masked = []
print("word is", word)

for letter in word:
if letter is 't' or letter is 'o':
masked.append(letter)
else:
masked.append('-')

output = ''.join(masked)
print(output)



As for the canvas, it depends what you mean by 'canvas' -- whichever GUI or graphics library should have a way to draw/write text on a canvas.

However, a canvas usually needs you to wipe it clean before doing "updates" because it's just a dumb collection of coloured pixels, so you'd either need to repaint the whole canvas from scratch every time something changes, or at least clean out a rectangular area which covers over the bit you want to update.
(wiping the canvas clean is usually less hassle -- you can just put all the code which paints/draws everything on your canvas into a def function which redraws the whole screen after the data changes)

Quick Reply

Latest

Trending

Trending