python3--基础综合练习题

发布时间:2018-04-07 17:14:32编辑:Run阅读(4943)

    最好自己先尝试做一做


    练习1:猜年龄游戏

    要求:

    允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出

    #!/usr/bin/env python
    # coding: utf-8
    __author__ = 'www.py3study.com'
    count = 0
    while count < 3:
        count += 1
        age = 23
        guess = input("请输入年龄:").strip()
        if int(guess) == age:
            print('恭喜你猜对了')
            break
        elif int(guess) < age:
            print('小了小了')
        else:
            print('大了大了')
        print('你还有{}次机会'.format(3-count))


    练习2:猜年龄游戏升级版

    要求:

    允许用户最多尝试3次

    每尝试3次,如果还没猜对,就问用户是否还想继续玩,如果回答y或Y,就继续让其猜3次,以此反复,如果回答N或n,就退出程序,如果猜对了,打印恭喜信息直接退出

    #!/usr/bin/env python
    # coding: utf-8
    __author__ = 'www.py3study.com'
    count = 0
    while count < 3:
        count += 1
        age = 23
        guess = input("请输入年龄:").strip()
        if int(guess) == age:
            print('恭喜你猜对了')
            break
        elif int(guess) < age:
            print('小了小了')
        else:
            print('大了大了')
        print('你还有{}次机会'.format(3-count))
        if count == 3:
            content = input("是否还要继续(Y/y继续),(N/n退出):").strip()
            if content.upper() == 'Y':
                count = 0  # 改变条件,让count = 0,又进入循环
            elif content.upper() == 'N':
                break


    练习3:写代码

    3.1:实现用户输入用户名和密码,当用户名为root且密码为123时,显示登陆成功,否则登陆失败

    #!/usr/bin/env python
    # coding: utf-8
    __author__ = 'www.py3study.com'
    while True:
        username = input('请输入账号:').strip()
        password = input('请输入密码:').strip()
        if username == 'root' and int(password) == 123:
            print('登陆成功!')  # 登陆成功需跳出循环
            break
        else:
            print('登陆失败')


    3.2:实现用户输入用户名和密码,当用户名为root且密码为123时,显示登陆成功,否则登陆失败,失败时允许重复输入三次

    #!/usr/bin/env python
    # coding: utf-8
    __author__ = 'www.py3study.com'
    count = 0
    while count < 3:
        username = input('请输入账号:').strip()
        password = input('请输入密码:').strip()
        if username == 'root' and int(password) == 123:
            print('登陆成功!')  # 登陆成功需跳出循环
            break
        else:
            count += 1
            print('登陆失败,还剩{}次机会'.format(3 - count))


    3.3:实现用户输入用户名和密码,当用户名为root或admin且密码为123时,显示登陆成功,否则登陆失败,失败时允许重复输入三次

    #!/usr/bin/env python
    # coding: utf-8
    __author__ = 'www.py3study.com'
    count = 0
    while count < 3:
        username = input('请输入账号:').strip()
        password = input('请输入密码:').strip()
        # or其中任意一个满足时,条件都成立,or和and同时出现时,
        # and的优先级大于or,所以需要()起来,()的优先级大于and
        if (username == 'root' or username == 'admin') and int(password) == 123:
            print('登陆成功!')  # 登陆成功需跳出循环
            break
        else:
            count += 1
            print('登陆失败,还剩{}次机会'.format(3 - count))


    练习4:

    4.1:使用while循环实现输出2-3+4-5+6....+100的和

    #!/usr/bin/env python
    # coding: utf-8
    __author__ = 'www.py3study.com'
    count = 2  # 定义一个初始值count=2,从2开始计算的
    sum1 = 0   # 定义一个sum1=0,用来计算
    while count <= 100:
        if count % 2 == 0:  # 求余数用%号
            sum1 += count  # 当count为偶数时,sum1 = sum1 + count
        else:
            sum1 -= count  # 当count为奇数时,sum1 = sum1 - count
        count += 1  # count 自加1,当count=100时,while循环结束
    print(sum1)


    4.2:使用while循环实现输出1,2,3,4,5,7,8,9,11,12

    #!/usr/bin/env python
    # coding: utf-8
    __author__ = 'www.py3study.com'
    count = 0
    while count < 12:
        count += 1
        # 当count=6 or 10的时候跳出本次循环执行下一次循环
        if count == 6 or count == 10:  
            continue
        print(count)


    4.3:使用while循环实现输出1-100内的所有奇数

    #!/usr/bin/env python
    # coding: utf-8
    __author__ = 'www.py3study.com'
    count = 0
    while count < 100:
        count += 1
        if count % 2 == 1:
            print(count)


    4.4:使用while循环实现输出1-100内的所有偶数

    #!/usr/bin/env python
    # coding: utf-8
    __author__ = 'www.py3study.com'
    count = 0
    while count < 100:
        count += 1
        if count % 2 == 0:
            print(count)


    练习5:

    基础需求

    1 让用户输入用户名密码

    2 认证成功后显示欢迎信息

    3 输错3次后退出程序

    4 可以支持多个用户登录(提示:通过列表存多个账户信息)

    5 用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)

    #!/usr/bin/env python
    # coding: utf-8
    __author__ = 'www.py3study.com'
    count = 0
    user_pwd = ['root', 123]
    with open('black_user', encoding='utf-8', mode='r') as f:
        lock_file = f.read()
    while count < 3:
        username = input("输入用户名:").strip()
        for i in lock_file:
            if lock_file == username:
                print('对不起,账号已锁定')
                exit()
        password = input("输入密码:").strip()
        if username == user_pwd[0] and int(password) == user_pwd[1]:
            print("欢迎{}登陆".format(user_pwd[0]))
            break
        else:
            count += 1
            print('登陆失败,还有{}次机会'.format(3 - count))
        if count == 3:
            print("对不起,你输入的密码错误次数已达到3次,账号已被锁定!")
            with open('black_user',encoding='utf-8', mode='a') as f:
                f.write('{}'.format('root'))
                break

    执行结果

    download.gif

关键字