Latest web development tutorials

Python 練習實例97

Python 100例 Python 100例

題目:從鍵盤輸入一些字符,逐個把它們送到磁盤上去,直到輸入一個#為止。

程序分析:無。

程序源代碼:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

if __name__ == '__main__':
    from sys import stdout
    filename = raw_input('input a file name:\n')
    fp = open(filename,"w")
    ch = raw_input('input string:\n')
    while ch != '#':
        fp.write(ch)
        stdout.write(ch)
        ch = raw_input('')
    fp.close()

以上實例輸出結果為:

input a file name:
a
input string:
b
b
c
c#

Python 100例 Python 100例