Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python for循环答案赢了';不要停止重复_Python_Python 3.x - Fatal编程技术网

Python for循环答案赢了';不要停止重复

Python for循环答案赢了';不要停止重复,python,python-3.x,Python,Python 3.x,我有这个计划: 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

我有这个计划:

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
循环就行了。你为什么有它?(你想重复什么?)


如果要替换字符串中的字母,请使用函数。不需要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))

我怀疑他们的目标是在每个单词的基础上进行计数和替换(因为提示是输入一个短语,即使他们只提供一个单词),但是的,问题并不清楚,他们没有努力将字符串拆分为一个单词列表。我怀疑OP想根据字母计数进行循环,正如暗影游侠所暗示的,这是一种可能性。
phrase = input('Enter a phrase:')
find = input('Enter a letter to find:')
end = input('Enter a letter to replace:')

print(phrase.replace(find, end))