美文网首页
零基础学Python 读《编程小白的第一本 Python 入门

零基础学Python 读《编程小白的第一本 Python 入门

作者: 像苏 | 来源:发表于2020-02-21 23:53 被阅读0次
    第五章练习题
    1. 设计这样一个函数,在桌面的文件夹上创建10个文本,以数字给它们命名。
    def create_txt():
        desktop_path = '/Users/su/Desktop/'
        for name in range(1, 11):
            full_path = desktop_path + '{}.txt'.format(name)
            file = open(full_path, 'w')
            file.close()
        print('Done')
    
    
    create_txt()
    
    #尝试用while循环解决问题
    def create_txt():
        desktop_path = '/Users/su/Desktop/'
        name = 11
        while name < 21:
            full_path = desktop_path + '{}.txt'.format(name)
            file = open(full_path, 'w')
            name = name + 1
            file.close()
        print('Done')
    
    
    create_txt()
    
    
    #书中给出的答案
    def text_creation():
        path = '/Users/su/Desktop/'
        for name in range(1, 11):
            with open(path + str(name) + ' .txt', 'w') as text:
                text.write(str(name))
                text.close()
                print('Done')
    
    
    text_creation()
    
    1. 复利是一件神奇的事情,正如富兰克林所说:“复利是能够将所有铅块变成金块的石头”。设计一个复利计算函数 invest(),它包含三个参数:amount(资金),rate(利率),time(投资时间)。输入每个参数后调用函数,应该返回每一年的资金总额。它看起来就应该像这样(假设利率为5%)
    def invest(amount, rate, time):
        print('principal amount:{}'.format(amount))
        for t in range(1, time + 1):
            amount = amount * (1 + rate)
            print('year {}: ${}'.format(t, amount))
    
    
    invest(100, 0.05, 3)
    
    1. 打印1~100内的偶数
    def even_print():
        for i in range(1, 100):
            if i % 2 == 0:
                print(i)
    
    print(even_print())
    
    综合练习
    1. 设计一个猜大小的小游戏
    import random
    def guess_big_small_game():
        yazhu = input('押注(big/small):')
        if yazhu == 'big' or yazhu == 'small':
            point1 = random.randrange(1, 7)
            point2 = random.randrange(1, 7)
            point3 = random.randrange(1, 7)
            a_list = [point1, point2, point3]
            print(a_list)
            daxiao = sum(a_list)
            if 3 <= daxiao <= 9:
                daxiao = 'small'
                if yazhu == daxiao:
                    print('<<<<<押对了>>>>>')
                else:
                    print('-----不对-----')
                    print('应该是',daxiao)
                guess_big_small_game()
            else:
                daxiao = 'big'
                if yazhu == daxiao:
                    print('<<<<<押对了>>>>>')
                else:
                    print('-----不对-----')
                    print('应该是',daxiao)
                guess_big_small_game()
        else:
            print('输入不对,重新输入')
            guess_big_small_game()
    guess_big_small_game()
    

    这个代码虽然可以跑通,但看答案以后才明白,分别定义摇骰子和判断大小的函数,才能实现后期加入金额的设定,下面是重新写的

    import random
    
    
    def roll_dice(num=3, points=None):
        print('<<<<< ROLL THE DICE! >>>>>')
        if points is None:
            points = []
        while num > 0:
            point = random.randrange(1, 7)
            points.append(point)
            num = num - 1
        return points
    
    
    def roll_result(total, num):
        isSmall = 1 * num <= total <= 3 * num
        isBig = 3 * num + 1 <= total <= 6 * num
        if isSmall:
            return 'small'
        elif isBig:
            return 'big'
    
    def start_game():
        print('<<<<< GAME STARTS! >>>>>')
        your_dice_num = int(input('输入需要骰子的数量:'))
        choice = ['big', 'small']
        your_choice = input('big or small:')
        points = roll_dice(your_dice_num)
        result = roll_result(sum(points), your_dice_num)
        youWin = your_choice == result
        if your_choice in choice:
            if youWin:
                print('你赢了', points)
            else:
                print('押错了',points)
                start_game()
        else:
            print('invaild wrong')
            start_game()
    start_game()
    

    以上加入了选择骰子的数量,但是还是不够完善

    练习题

    一、在最后一个项目的基础上增加这样的功能,下注金额和赔率。具体规则如下:

    • 初始金额为1000元;
    • 金额为0时游戏结束;
    • 默认赔率为1倍,也就是说押对了能得相应金额,押错了会输掉相应金额。
    • 加入了选择骰子的数量
    • 增加输入金额超出时的判断
    • 增加对输入big or small 时输入的判断
    import random
    
    
    def roll_dice(num=3, points=None):
        print('<<<<< ROLL THE DICE! >>>>>')
        if points is None:
            points = []
        while num > 0:
            point = random.randrange(1, 7)
            points.append(point)
            num = num - 1
        return points
    
    
    def roll_result(total, num):
        isSmall = 1 * num <= total <= 3 * num
        isBig = 3 * num + 1 <= total <= 6 * num
        if isSmall:
            return 'small'
        elif isBig:
            return 'big'
    
    
    def start_game():
        money = 1000
        print('<<<<< GAME STARTS! >>>>>')
        your_dice_num = int(input('输入需要骰子的数量:'))
        while money > 0:
            print('你现在有{}元'.format(money))
            duzhu = int(input('输入你的赌注:'))
            if duzhu <= money:
                choice = ['big', 'small']
                your_choice = input('input big or small:')
                if your_choice in choice:
                    points = roll_dice(your_dice_num)
                    result = roll_result(sum(points), your_dice_num)
                    youWin = your_choice == result
                    if your_choice in choice:
                        if youWin:
                            print('你赢了', points)
                            money = money + duzhu
                            print('账户金额:', money)
                        else:
                            print('押错了,它是', points)
                            money = money - duzhu
                            print('账户金额:', money)
                            if money == 0:
                                print('你没钱了,游戏结束')
                    else:
                        print('invaild wrong')
                        start_game()
                else:
                    print('big or small')
            else:
                print('输入的金额不对,请重新输入')
    
    
    start_game()
    

    二、我们在注册应用的时候,常常使用手机号作为账户名,在短信验证之前一般都会检验号码的真实性,如果是不存在的号码就不会发送验证码。检验规则如下:

    • 长度不少于11位;
    • 是移动、联通、电信号段中的一个电话号码;
    • 因为是输入号码界面,输入除号码外其他字符的可能性可以忽略;
    • 移动号段,联通号段,电信号段如下:
    CN_mobile = [134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705]
    CN_union = [130,131,132,155,156,185,186,145,176,1709]
    CN_telecom = [133,153,180,181,189,177,1700]
    
    def check_phone_num():
        CN_mobile = [134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 182, 183, 184, 187, 188, 147, 178, 1705]
        CN_union = [130, 131, 132, 155, 156, 185, 186, 145, 176, 1709]
        CN_telecom = [133, 153, 180, 181, 189, 177, 1700]
        phone_num = input('你的电话号码:')
        if len(phone_num) == 11:
            p_num = int(phone_num[:3])
            if p_num == 170:
                p_num = int(phone_num[:4])
                if p_num in CN_mobile:
                    print('移动号码,可以发信息了')
                elif p_num in CN_union:
                    print('联通号码,可以发信息了')
                elif p_num in CN_telecom:
                    print('电信号码,可以发信息了')
                else:
                    print('哪有这种号码,重输!')
                    check_phone_num()
            else:
                if p_num in CN_mobile:
                    print('移动号码,可以发信息了')
                elif p_num in CN_union:
                    print('联通号码,可以发信息了')
                elif p_num in CN_telecom:
                    print('电信号码,可以发信息了')
                else:
                    print('哪有这种号码,重输!')
                    check_phone_num()
        else:
            if len(phone_num) > 11:
                print('多了,是11位,重输!')
                check_phone_num()
            else:
                print('不够11位,重输!')
                check_phone_num()
    
    
    check_phone_num()
    

    电话号码这里的or应该这样用

    first_three in CN_mobile or first_four in CN_mobile
    
    def check_phont_num():
        while True:
            number = input('Enter your number :')
            CN_mobile = [134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 182, 183, 184, 187, 188, 147, 178, 1705]
            CN_union = [130, 131, 132, 155, 156, 185, 186, 145, 176, 1709]
            CN_telecom = [133, 153, 180, 181, 189, 177, 1700]
            first_three = int(number[:3])
            first_four = int(number[:4])
            if len(number) == 11:
                if first_three in CN_mobile or first_four in CN_mobile:
                    print('Operator:China Mobile')
                    print('We\'re sending verification code via text to your phone', number)
                    break
                elif first_three in CN_union or first_four in CN_union:
                    print('Operator:China Union')
                    print('We\'re sending verification code via text to your phone', number)
                    break
                elif first_three in CN_telecom or first_four in CN_telecom:
                    print('Operator:China Telecom')
                    print('We\'re sending verification code via text to your phone', number)
                    break
                else:
                    print('No such a operator')
            else:
                print('Invalid length,your number should be in 11 digits')
    check_phont_num()
    

    while ,只要输入的不对,就会一直重新输入

    相关文章

      网友评论

          本文标题:零基础学Python 读《编程小白的第一本 Python 入门

          本文链接:https://www.haomeiwen.com/subject/wwicqhtx.html