我正在尝试创建一个程序,用户必须将符号替换为字母才能获得10个解码的单词。我已设法使替换工作,但是当它更新时只显示一行,而不是全部。 这是我尝试将符号替换为字母的代码:
subs2=[]
for word in words_list:
tempword = (word)
tempword = tempword.replace('#','A')
tempword = tempword.replace('*', 'M')
tempword = tempword.replace('%', 'N')
addpair1=input("Enter a symbol you would like to replace:")
addpair2=input("What letter would you like to replace it with:")
tempword=tempword.replace(addpair1,addpair2)
print(tempword)
subs2.append(tempword)
print(subs2[0])
print(subs2[1])
print(subs2[2])
print(subs2[3])
print(subs2[4])
print(subs2[5])
print(subs2[6])
print(subs2[7])
print(subs2[8])
print(subs2[9])
然而,当我尝试替换符号时,会出现这种情况:
A+/084&"
A3MANA+
8N203:
,1$&
!-MN
.A7&33&
AMA71N
&-&641'2
A))85
9&330M
Enter a symbol you would like to replace:3
What letter would you like to replace it with:h
A+/084&"
Enter a symbol you would like to replace:
不是用替换重新打印所有编码的单词,而是简单地显示第一行。 我想知道是否有人知道我错过了什么或我的代码有什么问题。 任何帮助表示赞赏。
答案 0 :(得分:2)
如果我正确理解您的问题,您需要将input
步骤移出for
循环。这是你想要的:
words_list=["hi", "my","name","is"]
subs2=[]
addpair1=input("Enter a symbol you would like to replace:")
addpair2=input("What letter would you like to replace it with:")
for word in words_list:
tempword = (word)
tempword = tempword.replace('#','A')
tempword = tempword.replace('*', 'M')
tempword = tempword.replace('%', 'N')
tempword=tempword.replace(addpair1,addpair2)
print(tempword)
subs2.append(tempword)
print subs2
#Enter a symbol you would like to replace:"i"
#What letter would you like to replace it with:"j"
#hj
#my
#name
#js
#['hj', 'my', 'name', 'js']