Skip to content.

kagome.lab.tkikuchi.net

Sections
Personal tools
You are here: Home » Members » tkikuchi's Home » 授業 » 専門コア情報処理演習(2006) » 解答例13
Views

解答例13

Document Actions

問1

  • #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # a.py
    # いちご占い
    #
    f = open('fortune.dat')
    fortune = {}
    while 1:
        s = f.readline().strip()
        if not s:
            break
        key, value = s.split()
        fortune[key] = value
    f.close()
    #
    print '*** いちご占い ***'
    ans = raw_input('好きなフルーツの名前をどうぞ: ')
    if fortune.has_key(ans):
        print '%sの好きなあなたには%s.' % (ans, fortune[ans])
    else:
        print '%sの好きなあなたは健康に気をつけてください.' % ans
    

問2

  • #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # a.py
    # いちご占い
    #
    f = open('fortune.dat')
    fortune = {}
    while 1:
        s = f.readline().strip()
        if not s:
            break
        key, value = s.split()
        fortune[key] = value
    f.close()
    #
    print '*** いちご占い ***'
    while 1:
        ans = raw_input('好きなフルーツの名前をどうぞ: ')
        if not ans:
            print '********* さようなら *********'
            break
        if ans in fortune.keys():
            print '%sの好きなあなたには%s.' % (ans, fortune[ans])
        else:
            s = raw_input('%sのことは知りません。教えてください: ' % ans)
            fortune[ans] = s
    #
    f = open('fortune.dat', 'w')
    for key in fortune.keys():
        f.write('%s %s\n' % (key, fortune[key]))
    f.close()
    
Created by tkikuchi
Last modified 2007-01-22 21:27
 

Powered by Plone

This site conforms to the following standards: