python基本语法 运用(2)

让密码加密

imprt getpass
password=getpass.getpass("password:")

但是这种方式在pycharm中不好使,在命令行中可以
验证,用户名和密码

python基本语法 运用(2)_第1张图片

  • python中是强制缩进,节省代码,结构清晰
  • 上级,不用缩进,和他有关系的下级要缩进。就像上面的if和他的下一句,else和他的下一句
    -IndentationError就是缩进错误
    elif
    记住,python中没有else if 而是 elif
    这是一个猜数游戏
Liu_Yang=88
guess_age=int(input("guess age:"))
if guess_age==Liu_Yang:
    print("yes,you got it")
elif guess_age>Liu_Yang:
    print("think small")
else:
    print("think big")

改进 上面的游戏,让猜对或者超过3次就退出

Liu_Yang=88
count =0
while True:
    if count==3:
        break
    guess_age=int(input("guess age:"))
    if guess_age==Liu_Yang:
        print("yes,you got it")
        break
    elif guess_age>Liu_Yang:
        print("think small")
    else:
        print("think big")
        count+=1

进一步优化代码

Liu_Yang=88
count =0
while count<3:
    guess_age=int(input("guess age:"))
    if guess_age==Liu_Yang:
        print("yes,you got it")
        break
    elif guess_age>Liu_Yang:
        print("think small")
    else:
        print("think big")
        count+=1

进一步 让代码人性化一点

Liu_Yang=88
count =0
while count<3:
    guess_age=int(input("guess age:"))
    if guess_age==Liu_Yang:
        print("yes,you got it")
        break
    elif guess_age>Liu_Yang:
        print("think small")
    else:
        print("think big")
        count+=1
else:
    print("you have tried too many times ...fuck off")

对for的运用

Liu_Yang=88

for i  in range(3):
    guess_age=int(input("guess age:"))
    if guess_age==Liu_Yang:
        print("yes,you got it")
        break
    elif guess_age>Liu_Yang:
        print("think small")
    else:
        print("think big")
else:
    print("you have tried too many times ...fuck off")
  • 我们 跳过一个 打印一个,
for i in range(0,10,2):
    print("loop" ,i)
  • 输出结果:
    python基本语法 运用(2)_第2张图片

  • 这里若写for i in range(0,10,3):就是隔两个 跳一个 。它的意思是步长

  • 实现询问对方是否继续,在进行下一步 :

Liu_Yang=88
count =0
while count<3:
    guess_age=int(input("guess age:"))
    if guess_age==Liu_Yang:
        print("yes,you got it")
        break
    elif guess_age>Liu_Yang:
        print("think small")
    else:
        print("think big")
        count+=1
        if count==3:
            countine_confirm=input("do you want to keep?")
            if countine_confirm !='n':
                count =0

你可能感兴趣的:(python基础到开发)