99乘法表
first = 1
while first<=9:
sec = 1
while sec <= first:
print(str(sec)+ "*" + str(first) + "="+ str(sec * first) ,end="\t")
sec += 1
print()
first += 1
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
# /usr/bin/env python
# __author:abel
# date: 2017/10/13
# coding: utf-8
for i in range(1,10):
for j in range(1,i+1):
print('%d*%d=%d'% (i,j,i*j),end=' ')
print()
长方形
#coding:utf-8
#用户输入高度和宽度
height=int(input("Height:"))
width=int(input("Width:"))
num1_height=1
while num1_height <=height:
num_width = 1
while num_width <= width:
print("#",end="")
num_width +=1
print("#")
num1_height +=1
Height:4
Width:5
######
######
######
######
直角三角形
line = 1
while line<=5:
temp=1
while temp<=line:
print("*",end="")
temp +=1
print()
line +=1
*
**
***
****
*****
#猜年龄#猜年龄
age_of_prinacl = 56
guess_age = int(input(">>:"))
'''
if guess_age == age_of_prinacl then
print "yes"
else
print "no"
'''
if guess_age == age_of_prinacl:
print("Yes,your got it ..")
elif guess_age > age_of_prinacl:
print("Your guess is large")
elif guess_age < age_of_prinacl:
print("Your guess is samller.")
else:
print("No,it's wrong.")
购物
#!/usr/bin/env python
# coding: utf-8
product_list=[
('mac',9000),
('kindle',800),
('tesla',900000),
('python book',105),
('bike',2000),
]
saving=input('pls input your money: ')
shopping_car=[]
if saving.isdigit():
saving=int(saving)
while True:
for i,v in enumerate(product_list,1):
print(i,".",v)
choice=input('选择购买商品编号[退出: q]: ')
if choice.isdigit():
choice=int(choice)
if choice>0 and choice<=len(product_list):
p_item=product_list[choice-1]
if p_item[1]<saving:
saving-=p_item[1]
shopping_car.append(p_item)
else:
print("余额不足,还剩%s"%saving)
print(p_item)
else:
print('编码不存在')
elif choice=='q':
print('-------您已购买如下商品--------')
for i in shopping_car:
print(i)
print('您还剩%s元钱'%saving)
break
else:
print('invalid input')
三级菜单
# /usr/bin/env python
# __author:abel
# date: 2017/10/12
# coding: utf-8
menu={
'北京':{''
'朝阳':{
'国贸':{
'CICC':{},
'HP':{},
'渣打银行':{},
'CCTV':{},
},
'望京':{
'陌陌':{},
'奔驰':{},
'360':{},
},
'三里屯':{
'优衣库':{},
'苹果':{},
},
},
'昌平':{
'沙河':{
'老男孩':{},
'阿泰包子':{},
},
'天通院':{
'链家':{},
'我爱我家':{},
},
'回龙观':{},
},
'海淀':{
'五道口':{
'谷歌':{},
'网易':{},
'搜狐':{},
'搜狗':{},
'快手':{},
},
'中关村':{
'优酷':{},
'爱奇艺':{},
'汽车之家':{},
'新东方':{},
'腾讯':{},
},
},
},
'上海':{
'浦东':{
'陆家嘴':{
'CICC':{},
'高盛':{},
'莫更':{},
},
'外滩':{},
},
'闵行':{},
'静安':{},
},
'山东':{
'济南':{},
'德州':{
'乐林':{
'丁坞镇':{},
'城区':{},
},
'平原县':{},
},
'青岛':{},
},
}
current_layer = menu #实现动态循环
# parent_layer = menu
parent_layers = [] #保存所有父集,最后一个元素都是父亲级
while True:
for key in current_layer:
print(key)
choice= input(">>:").strip()
if len(choice) ==0:continue
if choice in current_layer:
parent_layers.append(current_layer) #在进入下一层之前,把当前层(也就是下一层父级追加到列表中)
#下一次loop,当用户先择b的选择,就可以直接取列表的最后一个值出来就OK了
current_layer=current_layer[choice]
elif choice == 'b':
if parent_layers:
current_layer=parent_layers.pop()#取出列表的最后一个值,因为它就是当前层的父级
else:
print('无此项')
格式还输出
# __author:abel
# date: 2017/9/30
# coding: utf-8
name = input("Name:")
age = int(input("Age:"))
job = input("Job:")
salary = input("Salary:")
if salary.isdigit():
salary = int(salary)
else:
# print( )
exit("Your input is not a number!")
msg = '''
--------info of %s---------
Name:%s
Age :%s
Job :%s
Salary:%s
You will be retired in %s years
----------- end --------------
''' % (name,name ,age ,job ,salary, 65-age)
print(msg)
运行结果
Name:xx
Age:45
Job:IT
Salary:10000
--------info of xx---------
Name:xx
Age :45
Job :IT
Salary:10000
You will be retired in 20 years
----------- end --------------
百钱买百鸡
# coding:utf-8
for x in range(1,20):
for y in range(1,33):
z=100-x-y
if (z % 3 ==0) and (x * 5 + y * 3 + z / 3 ==100 ):
s="公鸡: %d只: 母鸡: %d只: 小鸡: %d只: " %(x,y,z)
print(s)
运行结果
公鸡: 4只: 母鸡: 18只: 小鸡: 78只:
公鸡: 8只: 母鸡: 11只: 小鸡: 81只:
公鸡: 12只: 母鸡: 4只: 小鸡: 84只:
邮件
from email.mime.text import MIMEText
msg=MIMEText('hello,send by Python...','plain','utf-8')
from_addr=input('From:')
password=input('Password:')
to_addr=input('To:')
smtp_server=input('SMPT server:')
import smtplib
server=smtplib.SMTP(smtp_server,25)
server.set_debuglevel(1)
server.login(from_addr,password)
server.sendmail(from_addr,[to_addr],msg.as_string())
server.quit()