1. Trang chủ
  2. » Công Nghệ Thông Tin

Code python tạo một playlist với nhiều video

8 6 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 8
Dung lượng 98,51 KB

Nội dung

Về cơ bản, đây là một ứng dụng Console đơn giản để quản lý thông tin về video và playlist, cho phép người dùng nhập và lưu thông tin một cách dễ dàng. Bên cạnh đó, sẽ có những phần khá là khó hiểu do đó sẽ có chú thích và giải thích bên dưới các bạn nhớ theo dõi nha. Mình sẽ ra bài code liên tục

Về bản, ứng dụng Console đơn giản để quản lý thông tin video playlist, cho phép người dùng nhập lưu thông tin cách dễ dàng class Video: def init (self, title, link): self.title = title self.link = link class Playlist: def init (self, name, description, rating, videos): self.name = name self.description = description self.rating = rating self.videos = videos def read_video(): title = input("Enter title: ") link = input("Enter link: ") video = Video(title, link) return video def print_video(video): print("Video title: ", video.title, end="") print("Video link: ", video.link, end="") def read_videos(): videos = [] total_video = int(input("Enter how many videos: ")) for i in range(total_video): print("Enter video ", i+1) vid = read_video() videos.append(vid) return videos def print_videos(videos): for i in range(len(videos)): print_video(videos[i]) def write_video_txt(video, file): file.write(video.title + "\n") file.write(video.link + "\n") def write_videos_txt(videos, file): total = len(videos) file.write(str(total) + "\n") for i in range(total): write_video_txt(videos[i], file) def read_video_from_txt(file): title = file.readline() link = file.readline() video = Video(title, link) return video def read_videos_from_txt(file): videos = [] total = file.readline() for i in range(int(total)): video = read_video_from_txt(file) videos.append(video) return videos def read_playlist(): playlist_name = input("Enter playlist name: ") playlist_description = input("Enter playlist description: ") playlist_rating = input("Enter rating (1-5): ") playlist_videos = read_videos() playlist = Playlist(playlist_name, playlist_description, playlist_rating, playlist_videos) return playlist def write_playlist_txt(playlist): with open("data.txt", "w") as file: file.write(playlist.name + "\n") file.write(playlist.description + "\n") file.write(playlist.rating + "\n") write_videos_txt(playlist.videos, file) print("Successfully write playlist to txt") def read_playlist_from_txt(): with open("data.txt", "r") as file: playlist_name = file.readline() playlist_description = file.readline() playlist_rating = file.readline() playlist_videos = read_videos_from_txt(file) playlist = Playlist(playlist_name, playlist_description, playlist_rating, playlist_videos) return playlist def print_playlist(playlist): print(" -") print("Playlist name: " + playlist.name, end="") print("Playlist description: " + playlist.description, end="") print("Playlist rating: " + playlist.rating, end="") print_videos(playlist.videos) def main(): playlist = read_playlist() write_playlist_txt(playlist) playlist = read_playlist_from_txt() print_playlist(playlist) main() Giải thích Đây đoạn mã Python để đọc viết thông tin video playlist vào tệp văn Đầu tiên, định nghĩa hai lớp Video Playlist: class Video: def init (self, title, link): self.title = title self.link = link class Playlist: def init (self, name, description, rating, videos): self.name = name self.description = description self.rating = rating self.videos = videos Lớp Video có thuộc tính title link, đại diện cho tiêu đề liên kết video Lớp Playlist bao gồm thuộc tính name, description, rating danh sách video liên quan đến Sau đó, khai báo hàm để đọc viết thông tin vào tệp văn bản: def read_video(): title = input("Enter title: ") link = input("Enter link: ") video = Video(title, link) return video def print_video(video): print("Video title: ", video.title, end="") print("Video link: ", video.link, end="") def read_videos(): videos = [] total_video = int(input("Enter how many videos: ")) for i in range(total_video): print("Enter video ", i+1) vid = read_video() videos.append(vid) return videos def print_videos(videos): for i in range(len(videos)): print_video(videos[i]) def write_video_txt(video, file): file.write(video.title + "\n") file.write(video.link + "\n") def write_videos_txt(videos, file): total = len(videos) file.write(str(total) + "\n") for i in range(total): write_video_txt(videos[i], file) def read_video_from_txt(file): title = file.readline() link = file.readline() video = Video(title, link) return video def read_videos_from_txt(file): videos = [] total = file.readline() for i in range(int(total)): video = read_video_from_txt(file) videos.append(video) return videos def read_playlist(): playlist_name = input("Enter playlist name: ") playlist_description = input("Enter playlist description: ") playlist_rating = input("Enter rating (1-5): ") playlist_videos = read_videos() playlist = Playlist(playlist_name, playlist_description, playlist_rating, playlist_videos) return playlist def write_playlist_txt(playlist): with open("data.txt", "w") as file: file.write(playlist.name + "\n") file.write(playlist.description + "\n") file.write(playlist.rating + "\n") write_videos_txt(playlist.videos, file) print("Successfully write playlist to txt") def read_playlist_from_txt(): with open("data.txt", "r") as file: playlist_name = file.readline() playlist_description = file.readline() playlist_rating = file.readline() playlist_videos = read_videos_from_txt(file) playlist = Playlist(playlist_name, playlist_description, playlist_rating, playlist_videos) return playlist Hàm read_video() đọc thông tin video từ người dùng, hàm write_video_txt() ghi thông tin video vào tệp văn Tương tự, có hàm đọc ghi thông tin cho nhiều video playlist (read_videos(), write_videos_txt(), read_videos_from_txt()) Hàm read_playlist() yêu cầu người dùng nhập thơng tin playlist sau tạo đối tượng Playlist tương ứng, gọi hàm read_videos() để đọc danh sách video playlist Hàm write_playlist_txt() ghi thông tin playlist vào tệp văn Cuối cùng, chạy hàm main(), đọc viết thông tin vào tệp văn in thông tin playlist: def main(): playlist = read_playlist() write_playlist_txt(playlist) playlist = read_playlist_from_txt() print_playlist(playlist) main()

Ngày đăng: 24/06/2023, 19:47

TỪ KHÓA LIÊN QUAN

w