简体   繁体   English

在弄清楚如何在单词之间实现空格时遇到麻烦。 现在我有下划线

[英]Having troubles figuring out how to implement spaces in between words. Right now I have underscores instead

My code is suppose to take a phrase from a file. 我的代码假设是从文件中提取一个短语。 I currently have it working, but I can't seem to figure out how I can change my output. 我目前可以使用它,但是我似乎无法弄清楚如何更改输出。 In my output I have underscores for spaces in between words. 在我的输出中,我在单词之间有下划线。 I want the output to have actual spaces instead of underscores. 我希望输出具有实际空格而不是下划线。 In my code I have each character replace with an underscore. 在我的代码中,我用下划线替换了每个字符。 I think this is my problem because it is reading the blank spaces as characters also. 我认为这是我的问题,因为它也在读取空格作为字符。 My goal is to figure out with some help on how I can't stop this from happening. 我的目标是在我无法阻止这种情况发生的情况下提供一些帮助。

Update: The problem was fixed so far. 更新:到目前为止,此问题已得到解决。 Now there is a underscore at the end of each phrase now. 现在,每个短语的末尾都有一个下划线。 Any suggestions? 有什么建议么?

    #Define a filename
filename = "puzzles.txt".lower()

#Imports
import random

#Open the file and read
with open(filename) as f:
    lines = f.readlines()
    randomSecretPhrase = (random.choice(lines)) #Choose random phrase from file
    #***Output secret word to screen test***
    print(randomSecretPhrase)

#Initalize        
correct = []
incorrect = []
guesses = []
count = 0

def game():
    for i in randomSecretPhrase:
        if i in correct:
            print(i,end=' ')
        elif i != ' ':
            print('_',end=' ')
        else:
            print(' ',end=' ')
    print("\n\n")
    print('Number of times guessed: ', count)
    print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~')

def guessed_letters():
#Will check the user input to verify valid letter is entered
    while True:
        guess = input("Guess a letter: ").lower()
        if guess in correct or guess in incorrect:
            print("You have already guessed that letter. Please guess again.")
            print(count)
        elif guess.isnumeric():
            print("Please enter only letters not numbers! Guess again.")
        elif len(guess) > 1:
            print("Please enter only one letter at a time. Please guess again.")
        elif len(guess) == 0:
            print("Please enter a letter.")
        else:
            break

#Keep track of correct and incorrect guesses

    if guess in randomSecretPhrase:
        correct.append(guess)
        print('~~~~~~~~~~~~~~~~~~~~~~~~')
        print("You have guessed correctly!")
        print('\n')
    else:
        print('~~~~~~~~~~~~~~~~~~~~~~~~')
        incorrect.append(guess)
        print("You have guessed incorrectly!")
        print('\n')

#Main program
while True:
    #Call game()
    game()
    #Call guessed_letters()
    guessed_letters()
    #Count how many times guesses
    count += 1






Output:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Guess a letter: h
~~~~~~~~~~~~~~~~~~~~~~~~
You have guessed correctly!


b a c k   i n   a   f l a s h _ 


Number of times guessed:  10
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

One of these will get rid of the underscores. 其中之一将摆脱下划线。 You are correct that it is iterating over the spaces as well. 您是正确的,它也在空间上进行迭代。

def game():
for i in randomSecretPhrase:
    if i in correct:
        print(i,end=' ')
    elif i in string.ascii_letters:
        print('_',end=' ')
    else:
        # Print an extra space between words
        print(' ')

Or, if you don't want to have extra spaces in between the words you could: 或者,如果您不希望单词之间有多余的空格,则可以:

def game():
for i in randomSecretPhrase.replace(' ', ''):
    if i in correct:
        print(i,end=' ')
    elif:
        print('_',end=' ')

You probably also want to prevent the player from guessing spaces. 您可能还想防止玩家猜测空格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我有一个数据集,其列是单词。 如何将相同的列相互添加? - I have a dataset whose columns are words. How can I add the same columns to each other? 我在弄清楚以下代码的工作方式时遇到了麻烦? - I'm having trouble with figuring out how the following code works? 我可以帮忙弄清楚如何在 Python 中制作时间戳吗? - Can I have help figuring out how to make timestamps in Python? 我有一个单词列表。 我想添加一个与每个单词相关联的计数器变量。 我该怎么做呢? - I have a list of words. I want to add a counter variable associated with each word. How do I do this? 我无法弄清楚如何自动进行 OAuth 身份验证以访问 Google Drive。 (Python) - I'm having trouble figuring out how to automate OAuth authentication to access Google Drive. (Python) 我在弄清楚这个压缩代码时遇到麻烦 - I'm having trouble figuring out this condensed code 在un子手游戏中如何隐藏单词之间的空格? - how do i unhide spaces between words in a hangman game? 我有一个错误,但我不知道是什么导致了它 - I have an error, but I am not figuring out what is causing it 试图在保持顺序和打印的同时颠倒单词但无法找出问题所在 - trying to reverse words while maintaining order and print but having trouble figuring out the problem 我想使用正则表达式检查名称中包含空格的文件,并用下划线替换空格 - I want to use a regex to check for files that have spaces in the name and replace the spaces with underscores
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM