变成小白的第一本Python入门书,第五章循环与判断课后题答案

1 设计这样一个函数,在桌面文件夹上穿件10个文本,以数字给它们命名


<pre name="code" class="python">account = 1
while account <= 10 :
    a='C://Users/Administrator.WQ-20160528LXHW/Desktop/'
    b=str(account)+'.txt'
    c=a+b
    file = open(c,'w')
    #file.write('hello world')
    file.close()
    account = account+1

2 复利是一件很神奇的事情,正如富兰克林所说。设计一个复利计算函数invest(),它包含三个参数:amount(资金),rate(利率),time(投资时间)。输入每个参数后调用函数,应该返回每一年的资金综合。塔卡起来就应该像这样(假设利率为5%)


 

ef invest(amount,rate,time):
    num=1
    while num<=time:
        amount=amount*(1+rate)
        print('year ' + str(num) + ': $' + str(amount))
        num=num+1


3 打印1~100内的偶数

for i in range(1,100,1):
    if (i%2 == 0):
        print (str(i)+' is odd!')

4 游戏开始,首先玩家选择big or small ,选择完成后开始要三个骰子计算总值、

import random
def roll_dice(numbers=3,points=None):
    print ('<<<<< ROLL THE DICE! >>>>>')
    if points is None:
        points =[]
    while numbers >0 :
       point = random.randrange(1,7)
       points.append(point)
       numbers = numbers - 1
    return  points


def roll_result(total):
    isBig = 11<=total <=18
    isSmall = 3<=total <=10
    if isBig:
        return 'Big'
    else:
        return 'Small'

def start_game(money):
    print ('<<<<< GAME STARTS! >>>>>')
    choices = ['Big' , 'Small']
    your_choice = input('Big or Small')
    bet = input('How much you wanna bet')
    if your_choice in choices:
        if bet <= money:
            points = roll_dice()
            total = sum(points)
            youWin = your_choice == roll_result(total)
            if youWin:
                print('The points are '+str(points) + ' you win!')
                money = money + bet
                print ('You gained '+str(bet)+',you have '+str(money)+'now!')
                start_game(money)
            else:
                print('The points are '+str(points) + ' you lose!')
                money = money - bet
                print ('You lose ' + str(bet) + ',you have ' + str(money) + 'now!')
                if money == 0:
                    print ('Game Over!')
                else:
                    start_game(money)
        else:
            print('Invalid bet')
            start_game(money)
    else:
        print('Invalid Words')
        start_game(money)

5 使用手机号作为账户名,在短信验证之前都会检验号码真实性,如果不存在的号码就不会发送验证码。

def check_number():
    number = input('Enter Your number:')
    length = len(str(number))
    moblie = ['134', '135', '136', '137', '138', '139', '150', '151', '152', '157', '158', '159', '182', '183', '184']
    moblie1 = ['187', '188', '147', '178', '1705']
    union =['130', '131', '132', '155', '156', '185', '186', '145', '176', '1709']
    telecom =['133','153','180', '181', '189', '177', '1700']
    a=0
    if length != 11:
        print ('Invalid length,your number should be in 11 digits')
    else:

        for i in range(1, len(moblie)):
            if moblie[i] in str(number):
                print ('Operator :China mobile')
                print ('We are sending verification code via text to your phone:'+str(number))
                a=1
        for i in range(1, len(moblie1)):
             if moblie1[i] in str(number):
                print ('Operator :China mobile')
                print ('We are sending verification code via text to your phone:'+str(number))
                a=1
        for i in range(1, len(union)):
            if union[i] in str(number):
                print ('Operator :China union')
                print ('We are sending verification code via text to your phone:' + str(number))
                a = 1
        for i in range(1, len(telecom)):
            if telecom[i] in str(number):
                print ('Operator :China telecom')
                print ('We are sending verification code via text to your phone:' + str(number))
                a = 1

    if a==0:
        print ('No such a operator')

check_number()


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值