python 作业(总)

"""week3
恺撒密码问题。恺撒密码是古罗马恺撒大帝用来对军事情报进行加解密的算法,它采用替换方法将信息中的每一个英文字符循环替换为字母表
序列中该字符后面的第三个字符,字母表的对应关系如下。
明文:abcdefghijklmnopqrstuvwxyz。密文:defghijklmnopqrstuvwxyzabc。可以采用如下方法计算明文字符对应的密文字符。
对于明文字符 p,其密文字符c满足条件:c=(p+3) mod 26。
假设用户可能使用的输入仅包含小写字母 a~z和空格,请编写一个程序,对输入的字符串用恺撒密码进行加密,其中空格不用进行加密处理。
"""
k = str(input())
n = len(k)
for i in range(n):
    if 'z' >= k[i] >= 'a':
        c = (ord(k[i]) - ord('a') + 3) % 26
        print(chr(c + ord('a')), end='')
    elif k[i] == ' ':  # 仅包含小写字母 a~z和空格
        print(k[i], end='')  # 空格不用进行加密处理

print()
"""week4
作业:
定义一个包含 20 个 1-50 间的随机整数的列表,
1.求这个列表的最大值、最小值、平均值(不能调用系统函数)
2.用 2种方法去掉重复数。
"""

import random

s = []
for i in range(20):
    s.append(random.randint(1, 50))
s.sort()
print(s)

print('最小值', s[0])
print('最大值', s[-1])
k = 0
for i in s:
    k += i
print('平均值', k / 20)
# 用 2种方法去掉重复数。
s = list(set(s))
print(s)
print('----or----')
# 方法二 for循环 不会改变顺序
c = []
for i in s:
    if i not in c:
        c.append(i)
print(c)


"""week5
# 求 《ASP.NET》价格最便宜的店,求列表中有哪几本书,求每本书的平均价格

books = [{"name": "C#", "price": 23.7, "store": "amaing"},
         {"name": "ASP.NET", "price": 44.5, "store": "amaing"},
         {"name": "C#", "price": 24.7, "store": "dd"},
         {"name": "ASP.NET", "price": 45.7, "store": "dd"},
         {"name": "C#", "price": 26.7, "store": "xh"},
         {"name": "ASP.NET", "price": 25.7, "store": "xh"}]
"""
books = [{"name": "C#", "price": 23.7, "store": "amaing"},
         {"name": "ASP.NET", "price": 44.5, "store": "amaing"},
         {"name": "C#", "price": 24.7, "store": "dd"},
         {"name": "ASP.NET", "price": 45.7, "store": "dd"},
         {"name": "C#", "price": 26.7, "store": "xh"},
         {"name": "ASP.NET", "price": 25.7, "store": "xh"}]
# 1、求《ASP.NET》价格最便宜的店
minprice = None
for book in books:
    if book['name'] == "ASP.NET":
        if minprice == None:
            minprice = book['price']
            index = books.index(book)
        if book['price'] < minprice:
            minprice = book['price']
            index = books.index(book)
print(minprice, books[index]['store'])

# 2、求列表中有哪几本书
name = set(i['name'] for i in books)
names = ','.join(name)
print("列表中有:", names)

# 3、求每本书的平均价格
average1 = 0
average2 = 0
len1 = 0
len2 = 0
for i in books:
    if i["name"] == "C#":
        average1 = average1 + i["price"]
        len1 = len1 + 1
    else:
        average2 = average2 + i["price"]
        len2 = len2 + 1

print("《ASP.NET》的平均价格:", average2 / len2)
print("《C#》的平均价格:", average1 / len1)

"""week6
IEEE和T IOBE是两大热门编程语言排行榜。截至2018年12月,IEEE榜排名前五的语言是:Python、C++、C、Java和C#。
TIOBE 榜排名前五的语言分别是:Java、C、Python、C++和VB.NET。请编程:
1、上榜的所有语言
2、两个榜单中同时出现的语言
3、只在IEEE榜中前五的语言
4、只在一个榜中出现的语言
"""
A = {'Python', 'C++', 'C', 'Java', 'C#'}
B = {'Java', 'C', 'Python', 'C++', 'VB.NET'}
k = A | B
print('上榜的所有语言:', *k)
k2 = A & B
print('两个榜单中同时出现的语言:', *k2)
k3 = A - B
print('只在IEEE榜中前五的语言:', *k3)
k4 = A ^ B
print('只在一个榜中出现的语言:', *k4)
"""week8
写函数,统计字符串中有几个字母,几个数字,几个空格,几个其他字符,并返回结果。
"""

def count(s):
    number = 0  # 数字
    letter = 0  # 字母
    blank = 0  # 空格
    other = 0  # 字符
    for c in s:
        if c.isdigit():
            number += 1
        elif c.isalpha():
            letter += 1
        elif c == ' ':
            blank += 1
        else:
            other += 1
    print('数字个数:', number, '字母个数:', letter, '空格个数:', blank, '其他字符个数:', other)
    return number, letter, blank, other


s = input()
count(s)

"""week12
按要求编写一个python应用程序:

1、定义一个类,描述一个矩形,包含有长、宽两种属性(封装),和计算面积方法。

2、编写一个类,继承自矩形类,同时该类描述
长方体,具有长、宽、高属性(封装),和计算体积的方法。

3、编写一个测试类,对以上两个类进行测试,
创建一个长方体,定义其长、宽、高,输出其底面积和体积。
"""

class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        area = self.length * self.width
        print("面积:", area)


class Cuboid(Rectangle):
    def __init__(self, length, width, high):
        Rectangle.__init__(self, length, width)
        self.high = high

    def volume(self):
        volume = self.length * self.width * self.high
        print("体积:", volume)


class Test:
    cuboid = Cuboid(4,5,6)
    cuboid.area()
    cuboid.volume()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值