[英]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.