zoukankan      html  css  js  c++  java
  • python-第01章02章节-密文,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的函数随笔

  • 相关阅读:
    matplotlib笔记(subplot)
    matplotlib笔记(plot)
    [时间序列处理]python中计算日期差
    Ubuntu 16.04 apt-get更换为国内阿里云源
    居然忘了range()的用法
    elasticsearch+kibana使用中踩的坑,持续更新中。
    Elasticsearch+Logstash+Kibana教程
    ubuntu下anaconda从清华镜像的下载安装及配置
    ubuntu 解决node 修改代码不能同步刷新的问题
    ubuntu18.04 解决sublimie不能使用中文
  • 原文地址:https://www.cnblogs.com/hanjiali/p/11296287.html
Copyright © 2011-2022 走看看