一道Python的问题
来源:百度知道 编辑:UC知道 时间:2024/06/18 03:54:13
这道题目应该不会很复杂, 只用到初级的几个代码. 多谢各位了.
Given user inputs a string, substring, and a
replacement string. Replace the substring with
the replacement string, if it exists and print the
new string. Otherwise just say that the substring
does not exist.
You may NOT use the find() and replace()
methods of a string
For example, if the user inputs the following:
– String: This is a string.
– Substring: a
– Replacement string: the
Your program should print: This is the string.
Hint: You will need loops, break statements, etc
Given user inputs a string, substring, and a
replacement string. Replace the substring with
the replacement string, if it exists and print the
new string. Otherwise just say that the substring
does not exist.
You may NOT use the find() and replace()
methods of a string
For example, if the user inputs the following:
– String: This is a string.
– Substring: a
– Replacement string: the
Your program should print: This is the string.
Hint: You will need loops, break statements, etc
#!/usr/bin/python
#simple with the func of replacement()
def tw0(string,sub,rep):
if sub not in string:
return 'error,please again'
else:
string=string.replace(sub,rep)
return string
if __name__ == '__main__':
x=raw_input('the string: ')
y=raw_input('substring: ')
z=raw_input('replacement string: ')
print tw0(x,y,z)
#这是直接从我以前做的python的题里拷贝来的,貌似是python核心编程上的题目,那时候编的很简单,异常处理什么的也没考虑,楼主想加的话,自己加吧
#楼主给的是英文题,也正好给我增长一下阅读理解,准备两天后的六级 :)...THX