I need to replace the characters of a word with underscores for a hangman game. For example, "apple" would become "_ _ _ _ _". This is my code:
import random
chosen_word = random.choice(word_list)
for letter in chosen_word:
str.replace(letter, "_ ")
The error message I’m receiving is
"File "main.py", line 17, in
str.replace(letter, "_")
TypeError: replace expected at least 2 arguments, got 1
As far as I can tell, there are two arguments. I’ve tried messing around with format and the arguments (letter, "_ "), but to be honest, I’m a total noob and have no idea what went wrong.
Thanks for reading.
>Solution :
In str.replace(), there is no string named str in your code.
import random
chosen_word = random.choice(word_list)
for letter in chosen_word:
chosen_word = chosen_word.replace(letter, "_ ")