Python 如何用破折号(空格除外)替换字符串中的字符

Python 如何用破折号(空格除外)替换字符串中的字符,python,Python,所以我现在正在制作一个(远未完成的)刽子手游戏。除了我尝试用下划线替换随机选取的单词外,其他一切都很顺利。正如我所希望的那样,代码会用下划线替换每个字符,但我不希望它用破折号替换字符串中的空格。例如,如果随机挑选的团队是“纽约喷气机”,python将其替换为“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu_ __uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

所以我现在正在制作一个(远未完成的)刽子手游戏。除了我尝试用下划线替换随机选取的单词外,其他一切都很顺利。正如我所希望的那样,代码会用下划线替换每个字符,但我不希望它用破折号替换字符串中的空格。例如,如果随机挑选的团队是“纽约喷气机”,python将其替换为“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu_ __uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

我不明白我做错了什么,我以为if语句可以解决问题,但事实并非如此

# doesn't replaces spaces with dash
if letter != " ":
  hide = "_ " * len(secret_word)
到目前为止所有的代码

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)
基本上对每个字母进行相同的计算。 (因为len(secret_word)与正在处理的当前字符无关)

您要做的是:

#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)
或者,阅读python中的正则表达式和string.replace()函数

基本上对每个字母进行相同的计算。 (因为len(secret_word)与正在处理的当前字符无关)

您要做的是:

#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)
或者,阅读python中的正则表达式和string.replace()函数。

正则表达式

import re
hide = re.sub(r'\S', '_', secret_word)
正则表达式

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

可以自己编写一个小助手函数,如:

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

可以自己编写一个小助手函数,如:

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

您能给出一个示例文本作为输入和预期输出吗?我仍然不清楚你到底想要什么。你能给出一个示例文本作为输入和预期输出吗?我还不清楚你到底想要什么。谢谢,这正是我想要的。我是否可以使用这种方法,使破折号、冒号、撇号和逗号不被替换?
如果[“”、“、”、“:“、”]:hide=hide+letter
中的字母可以添加为分支。谢谢,这正是我要找的。我是否可以使用此方法以不替换破折号、冒号、撇号和逗号?
如果[“”、“,”:“,““,””]:hide=hide+letter
中的字母可以添加为分支。