-密文,if else判断和while,for循环

Posted hanjiali

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了-密文,if else判断和while,for循环相关的知识,希望对你有一定的参考价值。

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的函数随笔

 

以上是关于-密文,if else判断和while,for循环的主要内容,如果未能解决你的问题,请参考以下文章

python基础之循环与迭代器

python学习八(while循环)

Python中for else和while else语句学习心得

while语句结构(for循环)

else配合while或者for循环只用注意点

python学习手册:第十三章——while循环和for循环