PYTHON核心编程第7章第6题

!/usr/bin/env python

coding=utf-8

"""
这是一个投资系统,必须值为:

股市行情显示器 所持有的股票 购买价格 当前价位
company numbers pay_price now_price
富博国际 1000 5 5.1
浩博国际 200 1 0.8
利已 100 1 1.3
沙巴 30 20 18

数据结构:
{所持有的股票:[公司名称,购买价格,当前价位]}

其他可选:

  1. 收益率
  2. 52周最高指数
  3. 最低指数

用户每次输入各列数字,输出一行,每行数据构成一行,总列表包含所有行。
输入完毕,提示用户进行对其中之一进行排序; 把该数据项

"""

total_List = []
total_Dict = {}

def touzi_input():

"""
将用户的输入转化为列表的方式
"""

while  True:

    company = raw_input("股票名称: ")
    numbers = int(raw_input("持有数量: "))
    pay_price = int(raw_input("PAY_PRI: "))
    now_price = int(raw_input("NOW_PRI: "))
    shouyi = ( now_price - pay_price ) * numbers

    myList = [company,numbers,pay_price,now_price,shouyi]
    total_List.append(myList)

    ask = raw_input("Add again? (Y/N): ")[0].lower()

    print "
股票名称    持有数量     购入价格   市场价位   我的收益"
    print "%s         %d           %d         %d       %d" % (company,numbers,pay_price,now_price,shouyi )
    if ask == 'n':
        break

    print total_List

def show_dict():
"""
这个方法主要是使用循环的方式,生成我们想要的字典
"""
message = """
(C)股票名称
(N)持有数量
(P)购入价格
(O)当前价位
(S)我的收益

请选择: """
mSor = raw_input(message).lower()

print "
老板你选择了 [%s]" % (mSor)

while True:

    if mSor == 'c':
        num = 0
    elif mSor == 'n':
        num = 1
    elif mSor == 'p':
        num = 2
    elif mSor == 'o':
        num = 3
    elif mSor == 's':
        num = 4
    else:
        mSor = raw_input("Try Again: ")

    for i in total_List:
        if i[num] in total_Dict.keys():
            total_Dict[i[num]].append([i[0],i[1],i[2],i[3],i[4]])
        else:
            total_Dict[i[num]] = [[i[0],i[1],i[2],i[3],i[4]]]
    break

print total_Dict

def show_total():
"""
对当前的总列表进行递归,然后显示结果
"""
print "股票名称 持有数量 购入价格 市场价位 我的收益"
for i in total_List:
print "%s %d %d %d %d" % (i[0],i[1],i[2],i[3],i[4])

def login():

print "欢迎登录股票投资系统"

def showmenu():

message = """

(1)添加投资项目
(2)查当前已投资项目
(3)查询投资
(4)退出系统
请输入: """

while True:
    prompt = raw_input(message)

    if prompt == '1' : touzi_input()
    if prompt == '2' : show_total()
    if prompt == '3' : show_dict()
    if prompt == '4' : break

if name == 'main':
showmenu()

原文地址:https://www.cnblogs.com/start0cheng/p/3667298.html