zoukankan      html  css  js  c++  java
  • 第一个 Python 程序

    看了一些基础的 Python 新手教程后,深深感觉到 Python 的简洁与强大,这是我的第一个 Python Demo。下面是完整代码与执行截图。

    代码:

    # encoding: utf-8
    '''
    @author: Techzero
    @email: techzero@163.com
    @time: 2014-4-30 下午1:31:04
    '''
    import os
    import sys
    import cPickle as p
    
    class Person:
        def __init__(self, name, email):
            '''Initializes the person's data.'''
            self.name = name
            self.email = email
    
    def create():
        """Create new person and input email"""
        global Persons
        try:
            name = raw_input("Please input name:")
            while Persons.has_key(name):
                name = raw_input("This name has already exist, please input new name:")
            email = raw_input("Please input Email:")
        except EOFError:
            print '
    EOF Error'
            sys.exit()
        Persons[name] = email
        print ""
    
    def delete():
        """Search person by name and delete"""
        global Persons
        try:
            name = raw_input("Please input the person's name you want to delete:")
        except EOFError:
            print '
    EOF Error'
            sys.exit()
        if Persons.has_key(name):
            del Persons[name]
            save()
        else:
            print "No one called",name,"!
    "
    
    def modify():
        """Search person by name and modify email"""
        global Persons
        try:
            name = raw_input("Please input the person's name you want to modify:")
            if Persons.has_key(name):
                del Persons[name]
                email = raw_input("Please input new email:")
                Persons[name] = email
                save()
            else:
                print "No one called",name,"!
    "
        except EOFError:
            print '
    EOF Error'
            sys.exit()
    
    def save():
        """Save Persons to file"""
        global Persons
        File = 'person.dat'
        f = file(File, 'w')
        p.dump(Persons, f)
        f.close()
        print "Operation Done!
    "
    
    def read():
        """Read person from file"""
        global Persons
        File = 'person.dat'
        if os.path.exists(File):
            f = file(File)
            Persons = p.load(f)
            f.close()
        else:
            File = 'person.dat'
            f = file(File, 'w')
            f.close()
    
    def display():
        """Display all persons in the dictionary"""
        global Persons
        for name, email in Persons.items():
            print "        ",name,email
        print ""
    
    def search():
        """Search person by name"""
        global Persons
        try:
            name = raw_input("Please input the person's name you want to search:")
        except EOFError:
            print '
    EOF Error'
            sys.exit()
        if Persons.has_key(name):
            print "        ",name,Persons[name],"
    "
        else:
            print "No one called",name,"!
    "
    
    def menu():
        """Display a menu to choose operation"""
        choose = "0"
        while True:
            #i = os.system("cls")
            print'''1----Create
    2----Delete
    3----Modify
    4----Search
    5----Display
    6----Exit'''
            try:
                choose = raw_input("Please choose an item(1-6):")
            except EOFError:
                print '
    EOF Error'
                sys.exit()
            if choose == "1":
                create()
            elif choose == "2":
                delete()
            elif choose == "3":
                modify()
            elif choose == "4":
                search()
            elif choose == "5":
                display()
            elif choose == "6":
                print "Thanks for using!"
                sys.exit()
            else:
                print ""
    
    Persons = {}
    read()
    menu()

    执行截图





    本文固定链接:http://www.itechzero.com/coding/python/python-development-with-eclipse-pydev-install-tutorial/,转载请注明出处。

  • 相关阅读:
    react注意事项
    小程序的页面滚动
    calc
    写好的vue项目怎么打包成uniapp形式
    处理其他系统过来的token.
    解析token
    iframe接受不同域名的token
    tree懒加载的使用,
    js防抖节流
    vue2.0和vue3.0的区别
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/3905502.html
Copyright © 2011-2022 走看看