用PYTHON输入输出字符串

简介:

这段好懂的,可以互动。

复制代码
import sys
import re

class BadEmployeeFormat(Exception):
    """Badly formatted employee name"""

def get_employee():
    """
    Retrieve user imformation.
    This method simply prompts the user for
    an empoyee's name and his current job title.
    """
    employee = input('Empoyee Name:')
    role = input("Empoyee's Role:")
    if not re.match(r'^.+\s.+',employee):
        raise  BadEmployeeFormat('Full Name Requested'
        'for records database.')
    return {'name':employee, 'role':role}

if __name__ == '__main__':
    employees = []
    print ('Enter you employee, EOF to Exit...')
    while True:
        try:
            employees.append(get_employee())
        except EOFError:
            print ()
            print ("Empoyee Dump")
            for number, employee in enumerate(employees):
                print ('Emp #%d: %s, %s' % (number+1,
                employee['name'], employee['role']))
            print ('Copyright Sign 2010, SuperCompany, Inc.')
            sys.exit(0)
        except BadEmployeeFormat as e:
            print (sys.stderr, 'Error: '+str(e))
复制代码

目录
相关文章
|
17小时前
|
Python
Python注意字符串和字节字面量
【5月更文挑战第7天】Python注意字符串和字节字面量
11 4
|
2天前
|
Python
【Python操作基础】——字符串
【Python操作基础】——字符串
|
2天前
|
Python
Python字符串和字节不要混淆str.format()和bytes.format()
【5月更文挑战第6天】Python字符串和字节不要混淆str.format()和bytes.format()
6 1
|
2天前
|
Python
Python字符串和字节使用正确的编码/解码
【5月更文挑战第6天】Python字符串和字节使用正确的编码/解码
6 2
|
2天前
|
存储 Python
python字符串和字节明确数据类型
【5月更文挑战第6天】python字符串和字节明确数据类型
8 2
|
3天前
|
Python
Python避免在字符串和字节之间混淆
【5月更文挑战第5天】Python避免在字符串和字节之间混淆
14 3
|
5天前
|
数据安全/隐私保护 开发者 Python
【Python 基础】检查字符串是否只包含数字和字母?
【5月更文挑战第8天】【Python 基础】检查字符串是否只包含数字和字母?
|
5天前
|
Python
【Python 基础】如何将一个字符串转化为全大写和全小写?
【5月更文挑战第8天】【Python 基础】如何将一个字符串转化为全大写和全小写?
|
5天前
|
机器学习/深度学习 存储 人工智能
python 字符串的三种定义方式
python 字符串的三种定义方式
11 1
|
7天前
|
Python Perl
Python中的字符串分析:判断字符串中是否包含字母
Python中的字符串分析:判断字符串中是否包含字母
10 0