3 个 Python 编程小练习

3 个 Python 小练习:(1)查询手机号的运营商;(2)猜点数;(3)PPT 文件转 PDF 文件。

Python 编程入门电子书可点击: 分享 | 1 本 Python 编程电子书

Python
3 个 Python 编程小练习

Phone Number Check

# -*- coding: utf-8 -*-
"""
Created on Tue Sep 25 23:24:19 2018

@author: Wengsway

"""

# phone number
def phone_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]
    your_number = input('Enter Your Number :')
    length = len(your_number)
    three = int(your_number[0:3])
    if length == 11 :
        if three in CN_mobile :
            print("Operator : CN_mobile")
            print("We are sending verification code via text to your phone : {}".format(your_number))
        elif three in CN_union :
            print("Operator : CN_union")
            print("We are sending verification code via text to your phone : {}".format(your_number))
        elif three in CN_telecom :
            print("Operator : CN_telecom")
            print("We are sending verification code via text to your phone : {}".format(your_number))
        else :
            print("No such a operator")
            phone_number()
    else :
        print("Invalid length,your number should be in 11 digits")
        phone_number()
phone_number()

Guess Bet

# -*- coding: utf-8 -*-
"""
Created on Mon Oct  1 10:32 2018

@author: Wengsway

"""

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):
    if 11 <= total <= 18 :
        result = 'Big'
    else :
        result = 'Small'
    return result

def betmoney():
    choices = ['Big','Small']
    money = 1000   
    while money > 0 :
          print('<<< Game Starts ! >>>')
          your_choice = input('Big or Small: ')
          if your_choice in choices:                  
                your_bet = int(input('How much you wanna bet ? - '))
                points = roll_dice()
                result = roll_result(sum(points))
                if result == your_choice :
                      print('The points are {} You Win !'.format(points))
                      money = money + your_bet
                      print('You gained {}, you have {} now'.format(your_bet,money))
                else:
                      print('The points are {} You Lose !'.format(points))
                      money = money - your_bet
                      print('You lost {}, you have {} now'.format(your_bet,money))
          else:
                print('Invalid Input')
    print('Gmae Over !')
betmoney()

PPT To PDF

# -*- coding: utf-8 -*-
"""
Created on Tue Apr  3 10:45:32 2018

@author: Wengsway

"""
import comtypes.client
import os
import re

def init_powerpoint():
    powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
    powerpoint.Visible =1
    return powerpoint
def ppt_to_pdf(powerpoint,inputFileName,outputFileName,formatType = 32):
    newoutputFileName=[]
    if outputFileName[-3:] != 'pdf':                                #如果文件名的后三位不是pdf,则再文件名后面加上.pdf
        outputFileName =outputFileName + '.pdf'
    deck = powerpoint.Presentations.Open(inputFileName)             #打开ppt文件
    newoutputFileName=outputFileName.replace('.pptx','')            #删除文件名中的.pptx并保存到新的列表中
    newoutputFileName=newoutputFileName.replace('.ppt','')          #删除文件名中的.ppt并保存到新的列表中
    deck.SaveAs(newoutputFileName,formatType)
    deck.Close()
def convert_files_in_folder(powerpoint,folder):
    files = os.listdir(folder)
    pptfiles = [f for f in files if f.endswith(('.ppt','.pptx'))]   #寻找文件内所有的ppt\pptx文件
    for pptfile in pptfiles:
        fullpath = os.path.join(cwd,pptfile)
        ppt_to_pdf(powerpoint,fullpath,fullpath)                    #调用ppt_to_pdf函数
if __name__=="__main__":
    powerpoint = init_powerpoint()
    cwd = os.getcwd()
    convert_files_in_folder(powerpoint,cwd)
相关文章
Python Course homework 1

这是 Python 课程的第一次作业

发表评论