从键盘输入一些字符,逐个把它们写到指定的文件,直到输入一个@为止。请完善代码。 示例1: 请输入文件名: out.txt 请输入字符串: Python is open.@ 执行代码后,out.txt文件中内容为: Python is open. 示例2:

admin2019-09-02  52

问题 从键盘输入一些字符,逐个把它们写到指定的文件,直到输入一个@为止。请完善代码。
示例1:
请输入文件名:
out.txt
请输入字符串:
Python
is
open.@
执行代码后,out.txt文件中内容为:
Python is open.
示例2:
请输入文件名:
out.txt
请输入字符串:
python@123
执行代码后,out.txt文件中内容为:
Python
filename=input(’’请输入文件名:\n’’)
fp=
ch=input(’’请输入字符串:\n’’)
while
if’@’in ch:
else:
fp.write(ch+’’’’)

fp.close()

选项

答案filename=input(’’请输入文件名:\n’’) fp=open(filename,’’w’’) ch=input(’’请输入字符串:\n’’) while ch!=’@’: if’@’in ch: t=ch.find(’’@’’) fp.write(ch[0:t]) break else: fp.write(ch+’’’’) ch=input(’’’’) fp.close()

解析
转载请注明原文地址:https://jikaoti.com/ti/MD50FFFM
0

相关试题推荐
最新回复(0)