-2

I am working on a basic project for a stickman game. My code so far is:

import random

list_of_words = ["Hello"]
word = str(random.choice(list_of_words))
char = int(len(word))

Since I am still working on it, I am only using 1 word instead of many which could complicate things more.

So how this should work is; there is a list of words. One of them gets randomly picked. Then it counts the amount of characters in the word. Lastly, it prints a certain number of underscores depending on the number of characters in the word.

In theory, if correct, it should work like this:

Word = Hello

Number of characters = 5

Result: _____ (5 underscores in one line)

Zesty
  • 7
  • 2
  • 3
    Perhaps `print('_'*len(word))`? – Nick May 29 '22 at 00:34
  • 1
    also, in code above, `str` and `int` conversions are basically redundant. – rv.kvetch May 29 '22 at 00:45
  • What do you mean by "a y"? Do you mean "that many"? – wjandrea May 29 '22 at 01:09
  • Do you mean hangman? When I hear "stickman game", I think Henry Stickmin or Stick Fight. If you are trying to do hangman, the algorithm for printing underscores is going to be more complicated than this. You'd want a `set` of guessed letters, to start. – wjandrea May 29 '22 at 01:09

1 Answers1

0

You can remove the call to str() and then generate a string of underscores that has the same length as the original string by using '_' * len(word).

import random

list_of_words = ["Hello"]
word = random.choice(list_of_words)
print('_' * len(word))

This outputs:

_____
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33