0

So I am currently making a (far from finished) hangman game. Everything is working well except for when I try to replace the randomly picked word with underscores. The code DOES replace every character with an underscore as I wanted, but I would like for it to NOT replace the spaces within the string with the dash. For example if the randomly picked team is "New York Jets" python replaces it as so "_ _ _ _ _ _ _ _ _ _ _ _" instead of " _ _ _ (space) _ _ _ _ (space) _ _ _ _"

I don't understand what I did wrong I thought the if statement would solve the problem but it doesn't.

# doesn't replaces spaces with dash
if letter != " ":
  hide = "_ " * len(secret_word)

All of the code so far

def play():
# uses underscores hide the word and to hold space for each character within the word
    hide = ''
    secret_word = ''
    tries = 0

# Welcomes user
    print("Welcome to hangman!!!")
    print("Let's get started!!!")
    # allows user to pick difficulty
    difficulty = input("What difficulty do you want to play the game on?\nEasy = 9 tries. Normal = 6 tries. Hard = 4 tries.")
    if difficulty == "easy" or "Easy":

    # allows users to pick a theme
        theme = input("Okay I'll pick a word, all you have to do is pick a theme :) \n Themes to pick from: History, Companies, Geography, Music, Movies, Celebrities, and Sports Team! ")

        # if the theme has a subtheme
        if theme == 'sports team' :
            sport = input("What type of sport? Your options are: Football, Baseball, Basketball, and Hockey.")
            if sport == "Football":

                # imports .txt file of list 
                file = open('NFLTeams.txt', 'r')
                NFL = file.readlines()

                # randomly picks a team from the list
                secret_word = random.choice(NFL)
                print(secret_word)

                #hides the word with underscores
                for letter in secret_word:

                    # doesnt replaces spaces with dash
                    if letter != " ":
                            hide = "_ " * len(secret_word)
                print(hide)
1
  • Can you give a sample text as an input and your expected output? Still not clear to me what exactly you want. Commented Nov 30, 2018 at 1:39

3 Answers 3

1
if letter != " ":
    hide = "_ " * len(secret_word)

basically does the same computation for every letter. (Since len(secret_word) is independent of the current character being processed )

What you want to do is :

#hides the word with underscore
hide = ""
for letter in secret_word:
    # doesnt replaces spaces with dash
    if letter != " ":
            hide = hide + "_"
    else:
            hide = hide + " "
print(hide)

Alternatively ,read up on regular expressions and string.replace() functions in python.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks that's exactly what i was looking for. Is there anyway I can use this method so that dashes, colons, apostrophes, and commas are not replaced?
If letter in [" ",",",":","'"]: hide = hide + letter can be added as a branch.
1

Regular expression?

import re
hide = re.sub(r'\S', '_', secret_word)

Comments

0

Could write yourself a little helper function like:

def dashify(str):
    return "".join("-" if char is not " " else " " for char in str)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.