Python 一个接一个地显示字母,其余的是点,空格是空格
Python reveal letters one by one where rest is dots and spaces are spaces
我正在做一个单词猜测器(用于 Discord)并且我有这个功能(我以前做的不同但在上一个问题中得到了一些帮助):
def hidden_word(self, keyword, counter=0):
hidden = len(keyword) - counter
dots = '.' * hidden
return keyword[:counter] + dots
这需要被猜测的单词(称为关键字)和要显示的字母数量(计数器)例如,如果关键字是“test”并且计数器是 2,则函数将 return“ te..
尽管如果关键字由多个单词组成,例如“test word”(计数器 2),函数将 return“te......”这里的问题是不清楚关键字是否由多个词组成
我理想的输出是“te.. ....”
经过一些研究,我认为这可以通过 re.sub
轻松解决,如下所示:
# this function is called every x seconds
helpword = [re.sub(r'\S', '.', keyword)] # Returns "te.. ...." but not easily modifiable
helpword[counter] = keyword[counter] # Replace a dot with the following letter in the keyword
给出:TypeError:'str'对象不支持项目分配,所以这不起作用。
我该怎么做?
p.s我的完整代码is here
您好,代码不是很漂亮,但是可以用
def hidden_word( keyword, counter=0):
first_word = keyword.split(' ')[0]
end=first_word[:counter]+"."*(len(first_word)-counter)+" "+" ".join(["."*len(i) for
i in keyword.split(' ')[1:]])
return end
hidden_word("hello world !",2)
通过用点有条件地替换字母来构造一个新字符串:
def hidden_word(self, keyword, counter=0):
hidden = len(keyword) - counter
dots = '.' * hidden
# use the letter if its positionally before counter OR if its a space
# else use a dot
return ''.join(c if (i<counter or c==' ') else '.' for i,c in enumerate(keyword))
for c in range(11):
print(hidden_word("","house boat",c))
输出:
..... ....
h.... ....
ho... ....
hou.. ....
hous. ....
house ....
house ....
house b...
house bo..
house boa.
house boat
使用 re.sub 是个好主意,您只需要进行一些格式化。
import re
def hidden_word(keyword, counter=0):
dots = re.sub("[^\s]", '.', keyword[counter:])
return keyword[:counter] + dots
hidden_word('test_word', counter=2)
[^\s] = 忽略空格的正则表达式,允许您用点替换每个字符,但保留空格
编辑:我的解决方案并不完美,因为将计数器设为 5 不会显示第 5 个字母,我将尝试使用正确的解决方案更新我的 post
我正在做一个单词猜测器(用于 Discord)并且我有这个功能(我以前做的不同但在上一个问题中得到了一些帮助):
def hidden_word(self, keyword, counter=0):
hidden = len(keyword) - counter
dots = '.' * hidden
return keyword[:counter] + dots
这需要被猜测的单词(称为关键字)和要显示的字母数量(计数器)例如,如果关键字是“test”并且计数器是 2,则函数将 return“ te..
尽管如果关键字由多个单词组成,例如“test word”(计数器 2),函数将 return“te......”这里的问题是不清楚关键字是否由多个词组成
我理想的输出是“te.. ....”
经过一些研究,我认为这可以通过 re.sub
轻松解决,如下所示:
# this function is called every x seconds
helpword = [re.sub(r'\S', '.', keyword)] # Returns "te.. ...." but not easily modifiable
helpword[counter] = keyword[counter] # Replace a dot with the following letter in the keyword
给出:TypeError:'str'对象不支持项目分配,所以这不起作用。
我该怎么做?
p.s我的完整代码is here
您好,代码不是很漂亮,但是可以用
def hidden_word( keyword, counter=0):
first_word = keyword.split(' ')[0]
end=first_word[:counter]+"."*(len(first_word)-counter)+" "+" ".join(["."*len(i) for
i in keyword.split(' ')[1:]])
return end
hidden_word("hello world !",2)
通过用点有条件地替换字母来构造一个新字符串:
def hidden_word(self, keyword, counter=0):
hidden = len(keyword) - counter
dots = '.' * hidden
# use the letter if its positionally before counter OR if its a space
# else use a dot
return ''.join(c if (i<counter or c==' ') else '.' for i,c in enumerate(keyword))
for c in range(11):
print(hidden_word("","house boat",c))
输出:
..... ....
h.... ....
ho... ....
hou.. ....
hous. ....
house ....
house ....
house b...
house bo..
house boa.
house boat
使用 re.sub 是个好主意,您只需要进行一些格式化。
import re
def hidden_word(keyword, counter=0):
dots = re.sub("[^\s]", '.', keyword[counter:])
return keyword[:counter] + dots
hidden_word('test_word', counter=2)
[^\s] = 忽略空格的正则表达式,允许您用点替换每个字符,但保留空格
编辑:我的解决方案并不完美,因为将计数器设为 5 不会显示第 5 个字母,我将尝试使用正确的解决方案更新我的 post