[英]for loop answer won't stop repeating
我有這個程序:
print('~Find and Replace~\n')
phrase = input('Enter a phrase:')
find = input('Enter a letter to find:')
end = input('Enter a letter to replace:')
for j in phrase:
j = phrase.count(find)
print(j)
結果如下:
~Find and Replace~
Enter a phrase:pythop
Enter a letter to find:p
Enter a letter to replace:x
2
2
2
2
2
2
我應該怎么做才能使其僅使用for循環打印一次?
只是擺脫for
循環。 你為什么有它? (您想重復什么?)
print('~Find and Replace~\n')
phrase = input('Enter a phrase:')
find = input('Enter a letter to find:')
end = input('Enter a letter to replace:')
j = phrase.count(find)
print(j)
如果要替換字符串中的字母,請使用str.replace()
函數。 不需要for循環。
phrase = input('Enter a phrase:')
find = input('Enter a letter to find:')
end = input('Enter a letter to replace:')
print(phrase.replace(find, end))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.