zoukankan      html  css  js  c++  java
  • Python入门 Day1-小练习

     1. 使用while循环输入 1 2 3 4 5 6 8 9 10

    i = 1
    while i <= 10:
        if i == 7:
            i += 1
            continue
        print(i)
        i += 1

      

    2. 求1-100的所有数的和

    #f方法1:
    i = 1
    sum = 0
    while i < 101:
        sum += i
        i+=1
    print(sum)
    
    #方法2:
    print(sum(range(1,101,1)))

    3. 输出 1-100 内的所有奇数

    #方法1:
    i = 1
    while i <= 100:
        print(i)
        i += 2
    # 方法2:
    i = 1
    while i <= 100:
        if i % 2 == 1:
            print(i)
        i += 1
    ##方法3:
    for i in range(1,100,2):
        print(i)

    4. 输出 1-100 内的所有偶数

    #方法1:
    i = 1
    while i <= 100:
        if i % 2 == 0:
            print(i)
        i += 1
    ##方法2
    for i in range(0,101,2):
        print(i)

    5. 求1-2+3-4+5 ... 99的所有数的和

    #方法1:
    i = 1
    sum = 0
    while i < 100:
        if i % 2 == 1:
            sum += i
        else:
            sum -= i
        i += 1
    print(sum)
    
    # #方法2:
    i = 1
    sum = 0
    sub = 0
    while i < 100:
        if i % 2 == 1:
            sum += i
        else:
            sub -= i
        i += 1
    print(sum+sub)
    
    #方法3:
    a = sum(range(1,100,2))
    b = sum(range(0,100,2))
    print(a-b)

    6. 登录程序

        要求:可以支持多用户登录,登录三次失败后要求确认是否继续验证

    input  username password
    
    li = [{'username':'alex','password':'SB'},
        {'username':'wusir','password':'sb'},
        {'username':'taibai','password':'nanshen'}
        ]
    
    cot = 1
    
        # print(i['username'],i['password'])
    while cot <= 3:
        Username = input('Your username: ')
        Password = input('Your password:')
        for i in li:
            #print(i['username'],i['password'])
            if Username == i['username'] and Password == i['password']:
                print('access success')
                cot = 4
                break
        else:
            print('Your username or password is wrong')
            cot += 1
            if cot == 4:
                con = input('错太多次了,Do you want to continue:')
                if con == 'Y' or con == 'y':
                    cot = 1
                else:
                    print('access denied')
                    break
    #

    再默写一个吧

    ##  在默写一遍
    
    ##########################################################################
    li = [{'username':'alex','password':'SB'},
        {'username':'wusir','password':'sb'},
        {'username':'taibai','password':'nanshen'}
        ]
    cot = 1
    while cot <= 3:
        Usernm = input('Username:')
        Passwd = input('Password:')
        for i in li:
            if Usernm == i['username'] and Passwd == i['password']:
                print('Welcome',i['username'])
                cot = 4
                break
        else:
            cot += 1
            if cot == 4:
                Asw = input('错太多次了, Try again(Y|N)?')
                if Asw == 'Y' or Asw == 'y':
                    cot = 1
                else:
                    print("这么多次也不对?拜拜喽")
            else:
                print('Your username or password is wrong,Try again')
  • 相关阅读:
    利用CSS计数函数counter()实现计数
    弹跳加载动画特效Bouncing loader
    HTML页面中解决内容元素随窗口变化布局变乱问题
    CSS中列表项list样式
    框模型中设置内容区域元素占地尺寸box-sizing属性
    PHP100视频教程-->视频下载
    HTML页面中5种超酷的伪类选择器:hover效果
    HTML中获取input中单选按钮radio数据(性别例子)
    HTML中 DOM操作的Document 对象详解(收藏)
    14、输入一个链表,从尾到头打印链表每个节点的值。
  • 原文地址:https://www.cnblogs.com/wangph/p/test1.html
Copyright © 2011-2022 走看看