-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
  • 1
  • 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
  • Please take the [tour], read [what's on-topic here](/help/on-topic), [ask], and the [question checklist](//meta.stackoverflow.com/q/260648/843953), and provide a [mre]. "Implement this feature for me" is off-topic for this site because SO isn't a free online coding service. You have to _make an honest attempt_, and then ask a _specific question_ about your algorithm or technique. – Pranav Hosangadi May 29 '22 at 01:08
  • 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
  • 13,997
  • 5
  • 12
  • 27