变量
__author__ = 'weihui'
# -*- coding:utf-8 -*-
name = "wh"
name2 = name
print ("名字:", name,name2)
name = "s"
print ("另一名字:",name,name2)
ab_cd_of = "变量命名用下划线"
PIE = 3.1415926
"常量定义"
if else 条件判断
__author__ = 'weihui'
_username = 'whz'
_passwd = '123456'
username = input ("username:")
passwd = input("passwd:")
print("在pycharm中不能用")
if _username == username and _passwd == passwd:
print("Welcome user {name} login...".format(name=username))
else:
print("invalid username")
字符编码的区别与介绍
__author__ = 'weihui'
print ("ASCII 255 1bytes")
print ("1980 GB2312 7445汉字")
print ("1995 most GBK1.0 21003汉字")
print ("2000 GB18030 27484 包括少数语种")
print ("Unicode 占2字节 统一码,万国码(英文不好存)")
print ("utf-8 存一个中文字符占3字节 英文1byte")
name = "python3直接支持中文"
print (name)
用户交互
__author__ = 'weihui'
name = input ("name:")
age = int(input ("age:"))
job = input("job:")
info = '''
-------info of %s-----
name:%s
age:%d
job:%s
''' % (name,name,age,job)
info2 = '''
-------info of {_name}-----
name:{_name}
age:{_age}
job:{_job}
'''.format(_name=name, _age=age, _job=job)
info3 = '''
-------info of {0}-----
name:{0}
age:{1}
job:{2}
'''.format(name,age,job)
print (info)
print (info2)
print(info3)
猜年龄
__author__ = 'weihui'
age_of_whz = 20
count = 0
while count<3:
guess_age = int(input("guess age:"))
if guess_age == age_of_whz:
print("yes,you got it.")
break
elif guess_age < age_of_whz:
print("guess older")
else:
print("guess smaller...")
count += 1
if count == 3:
try_again = input("try again ?")
if try_again != 'n':
count = 0
else:
print ("game over")
购物车
__author__ = 'weihui'
# -*- coding:utf-8 -*-
shoppings = [('compter',10000),('cellphone',2000),('food',100),('water',50)]
shop_list = []
salary = int(input("input your salary:"))
while True:
for index,item in enumerate(shoppings):
print(index,item)
choice = input("choose:")
if choice.isdigit():
choice = int(choice)
if choice >= 0 and choice< len(shoppings):
p_item = shoppings[choice]
if p_item[1] <= salary:
shop_list.append(p_item)
salary -= p_item[1]
print("added %s into shopping cart,your current balance is %s" % (p_item,salary))
else:
print("\033[41;1m余额只剩[%s],买不了\033[0m" % salary)
else:
print("%s is not exist" % choice)
elif choice == 'q':
print("------shopping list------")
for p in shop_list:
print(p)
print("current balance is %s" % salary)
exit()
else:
print("error")