Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"Replace expected at least 2 arguments" – trying to write script for hangman game, need to replace letters with underscores

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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, "_ ")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading