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

  • 相关阅读:
    sql-DDL, DML 常用语句
    7.8 Structured Streaming
    7.7 输出操作
    7.6 转换操作
    7.5 高级数据源---Kafka
    7.4 基本输入源
    7.3 DStream操作
    7.2 Spark Streaming
    7.1 流计算概述
    6.3 使用Spark SQL读写数据库
  • 原文地址:https://www.cnblogs.com/hanjiali/p/11296287.html
Copyright © 2011-2022 走看看