第一天学习代码

  • A+
所属分类:python学习
一.第一个程序hello world
print("hello world")
二.字符编码

--->ASCII 255 1bytes

---> 1980 gb2312 7K+

---> 1995 GBK1.0 2W+

---> 2000 GB18030 27K+

---> unicode 2bytes

---> utf-8 en:1bytes ,zh:3bytes

python 2.X 输入中文会出现报错 ,因为默认的编码是ASCII 需要在前面加入 # -*-  coding:utf-8 -*- 将ASCII转换成UTF-8来执行,python3.X默认就是UTF-8的无需更改

注释:#表示单行注释 '''注释内容'''或者"""注释内容"""

命名 :一般全大写表示常量

但将 msg='''注释内容'''或者msg="""注释内容""" print(msg)将打印出来

例如:

msg = ''' asdasd

asdasdas

aasdasd'''

print(msg)
三. 字符格式化输出
username = input("name:")   #python 2.X使用raw_input python2中的input输入当成一个变量需要先定义 但加入双引号就表示字符
password = input("password:")
age = int(input("age:"))   #因为input默认的是str 需要强制将str转换成int
job = input("job:")
info = '''
------info of %s ----
name:%s
pass:%s
age:%d
job:%s
'''%(username,username,password,age,job)

info2 = '''
------info of {_name} ----
name:{_name}
pass:{_pass}
age:{_age}
job:{_job}
'''.format( _name = username,
            _pass = password,
            _age = age,
            _job = job
            )
info3 = '''
------info of {0} ----
name:{0}
pass:{1}
age:{2}
job:{3}
'''.format(username,password,age,job)  #需要一一对应

print(info1)
print(info2)
print(info3)  
四.密文password
import getpass #导入加密密码模块

_user = "ssh"
_password = "123456"

# user = input("user:")
# password = input("password:")   #这样password是明文的

user = input("user:")
#password = getpass.getpass("password:")  #在pycharm会输入不了,其他没有问题
password = input("password:")
# print(user,password)

#if else
if _user == user and _password == password :        #python 强制缩进 节省字符 使代码更加清晰
    print("Wecome user {name} login...".format(name=user))
else:
    print("Invalid user or password!")
五.while 循环 while True 表示永远循环
count = 0
while count < 3:
    age = 56
    guess_age = int(input("guess age:"))
    if guess_age > age:
        print("gou ta")
    elif guess_age == age:
        print("god it.")
        break
    else:
        print("gou xiao")
    count = count + 1  # 或者 count +=1
    if count == 3:
       countine_confirm = input("do you want go to(/n)")
          if countine_confirm != "n":
              count = 0
else:       #当不满足while 的条件时执行
    print("you have tried too many times ..fuck off")
陆.for 循环
for i in  range(10):  #i表示临时变量 ,range(10)表示(0,1,2,3,4,5,6,7,8,9)
     print("loop:",i)

for i in range(0,10,2): #(0,10,2) 0表示开始 10, 表示结束 ,2 表示步长 默然步长为1
    print("loop:", i)


for i in range(3):
    age = 56
    guess_age = int(input("guess age:"))
    if guess_age > age:
        print("gou ta")
    elif guess_age == age:
        print("god it.")
        break
    else:
        print("gou xiao")
else:       #当不满足range(3) 的条件时执行
    print("you have tried too many times ..fuck off")
break:跳出当前循环
continue: 跳出本次循环,进入下一次循环
#作业一
#编写登入接口
#输入用户名密码
#认证成功后显示欢迎信息
#输错三次后进行锁定

_user = "zs"
_password = "1234"
count = 0
for_lock = open("lock.txt","r")
_for_lock = for_lock.read()
if _for_lock == "lock":
    print("你的帐号已经锁定..")
    for_lock.close()
else:
    while count < 3:
        user = input("请输入用户名:")
        password = input("请输入密码:")
        if _user == user and _password == password:
            print("欢迎%s先生回来" %user)
            break
        else:
            count +=1       #count +=1与count = count +1一样
            print("输入错误你还有%s次机会.." %(3-count))
            if count == 3:
                lock = open("lock.txt","w")
                lock.write("lock")
                lock.close()

#redme
#需要在本程序同一目录建立一个空白的lock.txt文件 当 lock.txt文件中的内容为lock表示锁定 
  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
ssh

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: