zoukankan      html  css  js  c++  java
  • python练习题

    1、写一个函数,可以完成任意指定整数的相加,并返回结果

    def addition(*args):
        sum = 0
        for item in args:
            sum = sum + item
        return sum
    
    
    if __name__ == '__main__':
        res = addition(1, 2, 3, 10)
        print(res)
    View Code

    2、创建一个名为User的类,其中包含属性 frirst_name和last_name有用户简介通常会存储的其他几个属性,

    在类User中定义一个名为describe_user()的方法,它打印用户信息摘要,

    再定义一个名为greet_user()的方法,它向用户发出个性化的问候,

    创建多个表示不同用户的实例,并对每个实例都调用上述两个方法

    '''
    创建一个名为User的类,其中包含属性 frirst_name和last_name
    有用户简介通常会存储的其他几个属性,在类User中定义一个名为
    describe_user()的方法,它打印用户信息摘要,再定义一个名为
    greet_user()的方法,它向用户发出个性化的问候,创建多个表示不同用户
    的实例,并对每个实例都调用上述两个方法
    '''
    
    
    class User:
    
        def __init__(self, fisrt_name, last_name, age):
            self.first_name = fisrt_name
            self.last_name = last_name
            self.age = age
    
        def describe_user(self,*args):
    
            print("这个人叫:{0}{1},年龄:{2}
    兴趣有:".format(self.first_name,self.last_name,self.age))
            for item in args:
                # print(item)
                print(item , end='')
            print('')
    
        def greet_user(self,content):
    
                print("亲爱的{0},{1}".format(self.first_name,content))
    
    
        @classmethod
        def welcome_info(cls):
            print("欢迎大家的到来,谢谢大家!!!祝大家每天开开心心")
    
        @staticmethod
        def wish():
            print("希望大家越来越好")
    
    
    if __name__ == '__main__':
        user = User("","",19)
        user.describe_user("看书","看电视")
        user.greet_user("欢迎你!!!")
        user.welcome_info()
    
        print(150*"-")
        user1 = User("", "", 19)
        user1.describe_user("看书", "看电视")
        user1.greet_user("今天你真漂亮!!!")
    
        print(150*"-")
        user1 = User("", "", 19)
        user1.describe_user("看书", "看电视")
        user1.greet_user("你看起来真精神")
    
        user.wish()
    View Code

    3、自动贩卖机:只接受1元、5元、10元的纸币或硬币;

    可以1块,5元,10元。最多不超过10块钱。饮料只有橙计、椰汁、矿泉水、早餐奶,售价分别是3.5,4,2,4.5写一个函数用来表示贩卖机的功能:用户投钱和选择饮料,并通过判断之后,给用户吐出饮料和找零。
    '''
    自动贩卖机:只接受1元、5元、10元的纸币或硬币;
    可以1块,5元,10元。
    最多不超过10块钱。
    饮料只有橙计、椰汁、矿泉水、早餐奶,售价分别是3.5,4,2,4.5
    写一个函数用来表示贩卖机的功能:用户投钱和选择饮料,并通过判断之后,给用户吐出饮料和找零。
    '''
    
    
    class VendingMachine:
    
        def chose_product(self):
            product_info = {"橙汁": 3.5, "椰汁": 4, "矿泉水": 2, "早餐奶": 4.5}
            products = product_info.keys()
    
            # 所选商品总金额
            goods_sum_amount = 0
            goods_info = []
            print("商品选择完成后,记得点击【OK】进行结账")
            while True:
                try:
                    if 0 <= goods_sum_amount < 10:
                        chose_product = input("请选择商品: ")
                        if chose_product in products:
                            # 查出所选商品的金额
                            goods_amount = product_info[chose_product]
                            goods_info.append(chose_product)
                            goods_sum_amount = goods_sum_amount + goods_amount
    
                            # print(goods_sum_amount)
                            # print(goods_info)
                        elif chose_product == 'OK' or chose_product == 'ok':
                            # print("你选择的商品总额为:{0}元".format(goods_sum_amount))
                            break
                        else:
                            print("没有该商品,请重新选择商品")
    
                    elif goods_sum_amount == 10:
                        # print("你选择的商品总额为:{0}元".format(goods_sum_amount))
                        break
                    else:
                        # print("你选择的商品总金额超过10元了,你的商品金额为{0}元".format(goods_sum_amount))
                        # print("你选择的商品总金额超过10元了,你的商品金额为元" + goods_sum_amount)
                        break
    
                except Exception as e:
                    print("错误信息:".format(e))
                    break
    
            print("您选择的商品有: ", end='')
            for item in goods_info:
                print(item, end='')
    
            return goods_sum_amount
    
        def insert_coin(self):
    
            # 可找零的金额
            bills = [0.5, 1, 5, 10]
            vending = VendingMachine()
            # 需付款金额
            need_to_pay = vending.chose_product()
    
            if 0 < need_to_pay <= 10:
                print("你所选商品总额为:{0}元,请完成付款".format(need_to_pay))
    
                # 实付款金额
                actual_pay = 0
    
                while True:
                    if actual_pay < need_to_pay:
                        please_insert_coin = input("请投币: ")
                        actual_pay = actual_pay + int(please_insert_coin)
                        # print("你已付金额{0}元".format(actual_pay))
                    elif actual_pay == need_to_pay:
                        print("应付款:{0}元, 实付款:{1}元。
    付款已完成,欢迎下次光临!!!".format(need_to_pay, actual_pay))
                        break
                    elif actual_pay > need_to_pay:
                        give_change = actual_pay - need_to_pay
                        print("你已付{0}元,进行找零:{1}元".format(actual_pay, give_change))
                        break
    
    
    
            elif need_to_pay == 0:
                print("你未选择商品,不需要付款")
    
            else:
                print("你选择商品总金额超过10元,请重新选择")
    
    
    if __name__ == '__main__':
        res = VendingMachine()
        # print(res.chose_product())
        res.insert_coin()
        # res.chose_product()
    View Code

    4、写函数,将姓名、性别,城市作为参数,并且性别默认为f(女)。如果城市是在长沙并且性别为女,则输出姓名、性别、城市,并返回True否则返回 False

    '''
    写函数,将姓名、性别,城市作为参数,并且性别默认为f(女)。
    如果城市是在长沙并且性别为女,则输出姓名、性别、城市,并返回True否则返回 False
    '''
    
    def user_info(name,city,sex='f'):
    
        if sex == '':
            print("用户信息:{0},性别为女,所在城市:{1}".format(name,city))
            return True
        else:
            print("该用户不是女的,可能是个男的")
            return False
    
    if __name__ == '__main__':
        user_info("李四","上海",'')
        user_info("李四", "上海", '')
    View Code

    5、定义一个函数,成绩作为参数传入。如果成绩小于60,则输出不及格。如果成绩在60到80之间,则输出良好;如果成绩高于80分,则输出优秀,如果成绩不在0-100之间,则输出成绩输入错误。check_score.py

    '''
    定义一个函数,成绩作为参数传入。
    如果成绩小于60,则输出不及格。
    如果成绩在60到80之间,则输出良好;
    如果成绩高于80分,则输出优秀,如果成绩不在0-100之间,则输出成绩输入错误
    '''
    
    
    def check_scorre(grade):
        if 0 <= grade < 60:
            print("你的成绩为{0},成绩不及格,加油哦!".format(grade))
        elif 60 <= grade < 80:
            print("你的成绩为{0},成绩良好,继续加油!".format(grade))
        elif 80 <= grade <= 100:
            print("你的成绩为{0},成绩优秀,棒棒哒!".format(grade))
        else:
            print("成绩为:{0},成绩输入错误".format(grade))
    
    
    if __name__ == '__main__':
        check_scorre(310)
        check_scorre(0)
        check_scorre(60)
        check_scorre(80)
        check_scorre(100)
    View Code

    6、3人和机器猜拳游戏写成一个类,有如下几个函数

    1)函数1: 选择角色1曹操 2张飞 3刘备
    2)函数2: 角色猜拳1剪刀 2石头 3布 玩家输入一个1-3的数字
    3)函数3: 电脑出拳随机产生1个1-3的数字,提示电脑出拳结果
    4)函数4: 角色和机器出拳对战,对战结束后,最后出示本局对战结果,xxx赢xxx输,然后提示用户是否继续?按y继续,按n退出
    5)最后结束的时候输出结果,角色赢几局、电脑赢几局、平局几次 ,游戏结束
     
     7、按照以下要求定义一个游乐园门票类,并创建实例调用函数,完成儿童和大人的总票价统计(人数不定,由你输入的人数个数来决定)
    1)平日票价100元
    2)周末票价为平日票价120%
    3)儿童半价
    '''
    按照以下要求定义一个游乐园门票类,并创建实例调用函数,完成儿童和大人的总票价统计(人数不定,由你输入的人数个数来决定)
    1)平日票价100元
    2)周末票价为平日票价120%
    3)儿童半价
    '''
    
    
    class ParkTicket:
    
        def __init__(self, name, age, play_date,draw_num=1):
            self.name = name
            self.age = age
            self.play_date = play_date
            self.draw_num = draw_num
    
        def park_ticket(self):
    
            park_ticket_price = 100
    
            if 0 < self.age < 12:
                if self.play_date == '周六' or self.play_date == '周日':
                    park_ticket_price = park_ticket_price * 1.2 * 0.5
                    print("今天是{0},你还是个孩子,单票价为:{1}".format(self.play_date, park_ticket_price))
                else:
                    park_ticket_price = park_ticket_price * 0.5
                    print("你还是个孩子,单票价为:{0}".format(park_ticket_price))
    
            elif 12 <= self.age <= 100:
                if self.play_date == '周六' or self.play_date == '周日':
                    park_ticket_price = park_ticket_price * 1.2
                    print("今天是{0},你是个成年人,单票价为:{1}".format(self.play_date, park_ticket_price))
                else:
                    park_ticket_price = park_ticket_price
                    print("你是个成年人,单票价为:{0}".format(park_ticket_price))
    
            else:
                print("你不能去游乐园玩!!!!")
    
            ticket_price_sum = self.draw_num * park_ticket_price
            print("你有{0}个人同行,总票价为{1}".format(self.draw_num,ticket_price_sum))
    
            return ticket_price_sum
    
    
    if __name__ == '__main__':
        pt = ParkTicket("李四", 20, '周六',10)
        pt.park_ticket()
    View Code

    8、操作excel

    1)利用 openpyxl写一个专门读取Exce里面测试数据的类
    2)结合课堂上老师讲解的单元测试方法,通过初始化函数传参的方法,完成单元测试
    3)提交操作Exce的类,test_suite ,test_http类(类名可以你自己定,我提供的是老师上课写的类名)
     
     
     
     
     
     
     
     
     

    越努力越幸运
  • 相关阅读:
    C 指针运算 指针访问数组
    C 字符串操作函数
    C putchar getchar
    C语言 指向函数的指针变量基础
    Openstack L2GW Plugin installation
    nova + ironic node
    cgroup
    ironic pxe tftp(二)Permission denied
    ironic bind port neutron port
    Failed to start OpenBSD Secure Shell server
  • 原文地址:https://www.cnblogs.com/lfang/p/14899118.html
Copyright © 2011-2022 走看看