我有一个字符序列'----',我想用“jaillir”中正确范围内的每个字母替换其中的每个'---'。你知道吗
我该怎么做? 这是我的密码
import random
with open ("lexique.txt", "r", encoding= "utf8") as a:
words = []
letters = []
tirets= []
for line in a:
ligne = line[:-1]
words.append(ligne)
choix = random.choice(words)
tiret = ('-'* len(choix))
print(tiret)
print(choix)
accompli = False
while not accompli:
lettre = input("Entrez une lettre du mot ")
for t in range(len(tiret)):
if lettre in choix:
tiret.replace(tiret[t], lettre[t])
print(tiret)
我认为您需要修复文件读取代码,尽管这不是问题,如下所示:
接下来用一个单词替换,我假设您的文本中的破折号将永远与单词长度相同,因此:
所以现在可以使用python的字符串.替换方法如下:
使用for循环(逐步替换):
相关问题 更多 >
编程相关推荐