作者:Carrie
出处:https://home.cnblogs.com/u/hanjiali
版权:本文版权归作者和博客园共有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任

1,编写用户名及密码然后加密

import getpass
username = input("username")
password = getpass.getpass("password")
print(username,password)

 

 变秘文需要有模块,调用标准库(import中有的,不需要安装的库,叫做标准库)

import getpass,可以将明文变成密文

import getpass
username = input("username")
password = getpass.getpass("password")
print(username,password)

2. 想要判断用户名和密码对不对,所以我们需要判断

username_ = "han"
password_ =  "123"
username = input("username:")
password = input("password:")
print(username,password)

if username_ == username and password_ == password:
    print("welcome  login...")
else:
    print("密码或者用户名错误.")

 python中没有分隔符,所以有相应的强制缩进,省的结束语。如果总显示错误,写的代码没问题,一定要检查缩进问题。

3.判断年龄的大小

age_ = 30
age = int(input("Enter your age:"))
if age == age_:
    print("you are right!")
elif age_ < age:
    print("It is high")
else :
    print("It is low!")

 在python中 if-eiif-lese的循环结构较为简单,在循环体中记得要加“:”

 4.while 循环

判断年龄想要加功能,让其可以猜想三次

while True:
    if count == 3:
        break
    age_ =30
    age = int(input("Enter your age:"))
    if age == age_:
        print("you are right!")
        break
    elif age_ < age:
        print("It is high")
    else:
        print("It is low!")

    count += 

 优化一下:

count = 0
while count < 3: age_ =30 age = int(input("Enter your age:")) if age == age_: print("you are right!") break elif age_ < age: print("It is high") else: print("It is low!") count += 1 if count == 3:#else: print("你打印了太多次,请重新登陆!")

 5.for循环

for i in range(10):
    print("loop",i)

 想要跳一个打印一个

for i in range(0,10,2):
    print("loop",i)

 

将while循环改为for循环

for i in range(3):
    age_ =30
    age = int(input("Enter your age:"))
    if age == age_:
        print("you are right!")
        break
    elif age_ < age:
        print("It is high")
    else:
        print("It is low!")

 range()的用法见python的函数随笔

 

posted on 2019-08-03 23:32  不吃葡萄楞吐皮  阅读(179)  评论(0编辑  收藏  举报