python大作业

 

程序填空 (2*15 = 30)

4)  What is the output of the following code?

Import math

value = 2

print(math.pow(value,value))

答案是4.0

5)  What is the output of the following code?

Import math

value = -2

print(math.fabs (value))

答案是2.0

6)  What is the output of the following code?

Import math

value = 5

print(math.factorial(value))

答案是120

7)  What is the output of the following code?

sampleList = ["Jon", "Kelly", "Jessa"]

sampleList . append("Scott")

print(sampleList)

答案是["Jon", "Kelly", "Jessa,Scott"]

8)  What is the output of the following code?

sampleList = ["Jon", "Kelly", "Scott" ,"Jessa"]

print(sampleList[2:])

列表截取函数

9)  What is the output of the following code?

myStr = “This is my string”

print(myStr[11:])

答案是string

10)  What is the output of the following code?

sampleList = ["Jon", "Kelly", "Jessa"]

sampleList . pop()

print(sampleList)

11)  What is the output of the following code?

myStr = “This is my string”

print(myStr.replace(“my”,”your”))

12) What is the output of the following code?

sampleList = ["Jon", "Kelly", "Jessa"]

print(len(sampleList))

答案是3

13) What is the output of the following code?

sampleDict = {“Name”:"Jon", “Age”:20}

print(sampleDict[“Name”])

答案是Jon

14) What is the output of the following code?

sampleDict = {“Name”:"Jon", “Age”:20}

print(len(sampleDict))

答案是2

15) What is the output of the following code?

sampleList = ["Jon", "Kelly", "Jessa"]

for sample in sampleList:

if sample == “Kelly”:

print(sample)

  1. 程序阅读(10*4 = 40)

(1)

tmp = 0

for i in range(1,101):

tmp += i

print ('The sum is %d' % tmp)

   (2)

    person = {"li":18,"wang":50,"zhang":20,"sun":22}

    m = 'li'

    for key in person.keys():

        if person[m] < person[key]:

            m = key

print (m,person[m])

(3)

n = 0

p = raw_input('input a octal number:\n')

for i in range(len(p)):

     n = n * 8 + ord(p[i]) - ord('0')

print(n)

不会

(4)

TRUE = 1

FALSE = 0

def SQ(x):

    return x * x

print ('如果输入的数字小于 50,程序将停止运行。')

again = 1

while again:

    num = int(input('请输入一个数字:'))

    print ('运算结果为: %d' % (SQ(num)))

    if SQ(num) >= 50:

        again = TRUE

    else:

        again = FALSE

(5)

def sum_recu(n):

    if n>0:

       return n * sum_recu(n-1)

    else:

       return 1

sum = sum_recu(5)

print(sum)

(6)

import string

s = input('请输入一个字符串:\n')

letters = 0

space = 0

digit = 0

others = 0

for c in s:

    if c.isalpha():

        letters += 1

    elif c.isspace():

        space += 1

    elif c.isdigit():

        digit += 1

    else:

        others += 1

print (letters,space,digit,others)

(7)

score = int(raw_input('输入分数:\n'))

if score >= 90:

    grade = 'A'

elif score >= 60:

    grade = 'B'

else:

    grade = 'C'

print '%d 属于 %s' % (score,grade)

(8)

print(list(map(lambda x: x*x,[y for y in range(10)])))

(9)

datas = []

for i in range(10):

    datas.append(int(input("Input a number:")))

for i in range(9):

     for j in range(i+1,10):

         if datas[j]<datas[i]:

             temp = datas[j]

             datas[j] = datas[i]

             datas[i] = temp    

print(datas)

(10)

sum = 0

for num in range(1,101):

    if num%3 == 0:

        sum += 1

print(sum)

三、编程题

1.求解 1! + 2! + ·· · · + 20!的值

  1. 设计一个简单的图书馆管理系统,要求包含以下功能:


2.1创建一个基类 Book,包含图书的基本信息(出版书名、作者、年份、ISBN等)。


2.2创建两个派生类 PrintedBook 和 EBook,分别表示纸质书和电子书,它们继承自 Book。
PrintedBook 需要额外包含图书的重量。
EBook 需要额外包含电子书的格式(PDF、EPUB等)。


2.3创建一个图书馆管理系统类 Library,包含以下功能:
添加新图书(纸质书或电子书)到库存。
删除库存中的图书。
检查库存中是否存在某本书。
显示图书馆的所有图书信息。
编写程序演示图书馆管理系统的基本操作,包括添加、删除图书以及查询库存信息

class Book:  
    def __init__(self, title, author,year, isbn):  
        self.title = title  
        self.author = author  
        self.year = year  
        self.isbn = isbn  
class PrintedBook(Book):  
    def __init__(self, title, author, year, isbn, weight):  
        super().__init__(title, author, year, isbn)  
        self.weight = weight
class EBook(Book):  
    def __init__(self, title, author, year, isbn, format):  
        super().__init__(title, author, year, isbn)  
        self.format = format  

class Library:  
    def __init__(self):  
        self.books = []  
    def add_book(self, book):  
        self.books.append(book)  
        print(f"成功添加了 {book.title} 到库存。")  
    def delete_book(self, title):  
        for book in self.books:  
            if book.title == title:  
                self.books.remove(book)  
                print(f"删除了 {title}。")  
                break  
        else:  
            print("库存没有查询到该书本。")  
    def check_book(self, title):  
        for book in self.books:  
            if book.title == title:  
                print(f"库存中有 {title}。")  
                return True  
        return False  
    def show_books(self):  
        print("当前库存的图书信息:")  
        for book in self.books:  
            print(f"书名:{book.title}, 作者:{book.author}, 出版年份:{book.year}, ISBN:{book.isbn}")  
            if isinstance(book, PrintedBook):  
                print(f"重量:{book.weight}")  
            elif isinstance(book, EBook):  
                print(f"格式:{book.format}")  
            print()  


# 演示图书馆管理系统的基本操作  
library = Library()
a=input()
if a=="添加纸质书":
    b=["c","d","e","f","g"]
    c=input()
    d=input() 
    e=input()
    f=input()
    g=input()
    b[0]=c
    b[1]=d
    b[2]=e
    b[3]=f
    b[4]=g
    library.add_book(PrintedBook(b[0],b[1],b[2],b[3],b[4]))  # 添加纸质书 
elif a=="添加电子书":
    b=["c","d","e","f","g"]
    c=input()
    d=input() 
    e=input()
    f=input()
    g=input()
    b[0]=c
    b[1]=d
    b[2]=e
    b[3]=f
    b[4]=g
    library.add_book(EBook(b[0],b[1],b[2],b[3],b[4]))  # 添加电子书 
elif a=="删除图书":
    b=["c","d","e","f","g"]
    c=input()
    b[0]=c
    library.delete_book("b[0]")  # 删除图书
elif a=="检查库存中是否存在某本书":
    b=["c","d","e","f","g"]
    c=input()
    b[0]=c
    library.check_book("b[0]") 
elif a=="显示图书馆的所有图书信息":
   library.show_books() # 显示图书馆的所有图书信息
#library.add_book(PrintedBook("Python编程", "John", 2020, "978-1-111-11111-1", 0.5))  # 添加纸质书  
#library.add_book(EBook("电子书示例", "Jane", 2021, "978-2-222-22222-2", "PDF"))  # 添加电子书  
#library.delete_book("Python编程")  # 删除图书
#library.check_book("电子书示例")  # 检查库存中是否存在某本书  
#library.show_books()  # 显示图书馆的所有图书信息

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值