Python 学习范例(一)

1.九九乘法表打印

源码:

#__author: Lobin
#__date:   2018/1/7
#九九乘法表
for i in range(1,10):
    # j=1
    for j in range(1,i+1):
        if not(i==j):
            print("%d*%d=%d"%(j,i,i*j),end="\t")
        else:
            print("%d*%d=%d"%(j,i,i*j),end="\n")

结果为:

1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

 

2.购物车

源码为:

#__author: Lobin
#__date:   2018/1/7
#商品列表
product_list=[
    ('IPHONEX',9000),
    ('PEN',50),
    ('BIKE',2000),
    ('BMW',500000),
    ('NOTEBOOK',8000)
]
money_rest=input("Pls Input Your Avalible Money:")
if money_rest.isdigit():
    money_rest=int(money_rest)
    product_buy=[]
    while True:
        for i,v in enumerate(product_list,1):
            print(i,"<<<",v)
        product_code=input("Pls Input Your Product Code[Quit: q]:")
        if product_code.isdigit():
            product_code=int(product_code)
            if product_code>0 and product_code<len(product_list)+1:
                if money_rest>product_list[product_code-1][1]:
                    money_rest=money_rest-product_list[product_code-1][1]
                    product_buy.append(product_list[product_code-1])
                    print("Your Avaliable Money is:%d"%money_rest)
                else:
                    print("You Have not Avaliable Money")
        elif product_code == "q":
            print("----You Have Quit---------------")
            for i in product_buy:
                print(i)
            break
        else:
            print("Invalid Input Code")
else:
    print("Invalid Input Money")

结果为:

Pls Input Your Avalible Money:70000
1 <<< ('IPHONEX', 9000)
2 <<< ('PEN', 50)
3 <<< ('BIKE', 2000)
4 <<< ('BMW', 500000)
5 <<< ('NOTEBOOK', 8000)
Pls Input Your Product Code[Quit: q]:1
Your Avaliable Money is:61000
1 <<< ('IPHONEX', 9000)
2 <<< ('PEN', 50)
3 <<< ('BIKE', 2000)
4 <<< ('BMW', 500000)
5 <<< ('NOTEBOOK', 8000)
Pls Input Your Product Code[Quit: q]:4
You Have not Avaliable Money
1 <<< ('IPHONEX', 9000)
2 <<< ('PEN', 50)
3 <<< ('BIKE', 2000)
4 <<< ('BMW', 500000)
5 <<< ('NOTEBOOK', 8000)
Pls Input Your Product Code[Quit: q]:5
Your Avaliable Money is:53000
1 <<< ('IPHONEX', 9000)
2 <<< ('PEN', 50)
3 <<< ('BIKE', 2000)
4 <<< ('BMW', 500000)
5 <<< ('NOTEBOOK', 8000)
Pls Input Your Product Code[Quit: q]:q
----You Have Quit---------------
('IPHONEX', 9000)
('NOTEBOOK', 8000)

Process finished with exit code 0

 

3.三级结构

源码为:

#__author: Lobin
#__date:   2018/1/8
dic_SSX={
    '山西':{
        '太原':{},
        '长治':{},
        '阳泉':{
            '平定':{
                '石门口':{},
                '张庄':{},
                '东回':{}
            },
            '盂县':{},
            '郊区':{}
        }
    },
    '北京':{},
    '河北':{}
}
dic_menu=dic_SSX
list_parent=[]
while True:
    for i in dic_menu:
        print(i)
    input_chioce=input("Please Input Your Chouce:[Q for QUIT][B for Previous]")
    if input_chioce in dic_menu.keys():
        list_parent.append(dic_menu)
        dic_menu=dic_menu[input_chioce]
    elif input_chioce=='q':
        break
    elif input_chioce=='b':
        if list_parent:
            dic_menu=list_parent.pop()
    else:
        print("Invalid Input Values")

结果为:

山西
北京
河北
Please Input Your Chouce:[Q for QUIT][B for Previous]山西
太原
长治
阳泉
Please Input Your Chouce:[Q for QUIT][B for Previous]阳泉
平定
盂县
郊区
Please Input Your Chouce:[Q for QUIT][B for Previous]平定
石门口
张庄
东回
Please Input Your Chouce:[Q for QUIT][B for Previous]b
平定
盂县
郊区
Please Input Your Chouce:[Q for QUIT][B for Previous]b
太原
长治
阳泉
Please Input Your Chouce:[Q for QUIT][B for Previous]q

Process finished with exit code 0

 

posted on 2018-01-08 23:20  可爱的春哥  阅读(253)  评论(0)    收藏  举报

导航