来10个有趣的python程序

时间: 2024-01-28 18:00:11 浏览: 27
1. 猜数字游戏 让用户猜一个1到100之间的数字,每次猜完后告诉用户是猜大了还是猜小了,直到猜中为止。 2. 黑白棋游戏 实现一个简单的黑白棋游戏,让用户和电脑对弈,每次操作后交替下棋,最终谁占领了更多的棋盘格子就胜利了。 3. 生成随机密码 根据用户输入的密码长度,生成一个随机密码并输出。 4. 生成随机数列 根据用户输入的数列长度,生成一个随机的数列并输出。 5. 统计单词出现次数 读取一个文本文件,并统计其中每个单词出现的次数,最后输出出现频率最高的前十个单词。 6. 生成随机验证码 生成一个包含数字和字母的随机验证码,并输出。 7. 生成二维码 根据用户输入的字符串,生成一个二维码并输出。 8. 发送邮件 根据用户输入的收件人邮箱、邮件主题和邮件内容,发送一封邮件。 9. 翻译文本 根据用户输入的文本和目标语言,使用Google翻译API将文本翻译成目标语言并输出。 10. 生成随机颜色 生成一个随机的RGB颜色,并输出。
相关问题

有趣的python程序

以下是一些有趣的Python程序: 1. 猜数字游戏: ```python import random secret_number = random.randint(1, 100) guess = 0 tries = 0 print("Welcome to the Guess That Number game!") print("I'm thinking of a number between 1 and 100.") print("You have 10 tries to guess it.\n") while guess != secret_number and tries < 10: guess = int(input("Enter your guess: ")) if guess < secret_number: print("Too low.") elif guess > secret_number: print("Too high.") tries += 1 if guess == secret_number: print("\nCongratulations! You guessed my number in", tries, "tries!") else: print("\nSorry, you failed to guess my number. It was", secret_number) ``` 2. 计算器程序: ```python def add(x, y): return x + y def subtract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): return x / y print("Select operation.") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide") while True: choice = input("Enter choice (1/2/3/4): ") if choice in ('1', '2', '3', '4'): num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) if choice == '1': print(num1, "+", num2, "=", add(num1, num2)) elif choice == '2': print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '3': print(num1, "*", num2, "=", multiply(num1, num2)) elif choice == '4': print(num1, "/", num2, "=", divide(num1, num2)) break else: print("Invalid Input") ``` 3. 矩阵乘法程序: ```python def matrix_multiplication(X, Y): result = [] for i in range(len(X)): row = [] for j in range(len(Y[0])): sum = 0 for k in range(len(Y)): sum += X[i][k] * Y[k][j] row.append(sum) result.append(row) return result X = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Y = [[10, 11, 12], [13, 14, 15], [16, 17, 18]] result = matrix_multiplication(X, Y) for row in result: print(row) ``` 4. 无限制斐波那契数列生成器: ```python a, b = 0, 1 while True: yield a a, b = b, a + b ``` 5. 简单的爬虫程序: ```python import requests import re url = 'https://www.wikipedia.org/' response = requests.get(url) if response.status_code == 200: pattern = re.compile('<title>(.*?)</title>') title = re.search(pattern, response.text) print(title.group(1)) else: print("Error:" + response.status_code) ``` 这些程序可以帮助你理解Python语言的特点和功能。如果你对Python编程感兴趣,可以继续学习并编写更复杂的程序。

给我一个有趣的Python程序,要求可以在Python3.10运行

好的,这里是一个有趣的Python程序,可以在Python3.10运行,它可以模拟一个简单的扫雷游戏。 ```python import random class Minesweeper: def __init__(self, size=10, mines=20): self.size = size self.mines = mines self.board = [[0 for i in range(size)] for j in range(size)] self.gameover = False self.win = False self.revealed = [[False for i in range(size)] for j in range(size)] self.flagged = [[False for i in range(size)] for j in range(size)] def get_adjacent_cells(self, x, y): cells = [] for i in range(max(0, x-1), min(x+2, self.size)): for j in range(max(0, y-1), min(y+2, self.size)): if i != x or j != y: cells.append((i, j)) return cells def generate_board(self, x, y): positions = [(i, j) for i in range(self.size) for j in range(self.size)] for i in range(self.mines): position = random.choice(positions) positions.remove(position) if position == (x, y): continue self.board[position[0]][position[1]] = -1 for cell in self.get_adjacent_cells(position[0], position[1]): if self.board[cell[0]][cell[1]] != -1: self.board[cell[0]][cell[1]] += 1 def reveal_cell(self, x, y): if self.flagged[x][y]: return if self.board[x][y] == -1: self.gameover = True return self.revealed[x][y] = True if self.board[x][y] == 0: for cell in self.get_adjacent_cells(x, y): if not self.revealed[cell[0]][cell[1]]: self.reveal_cell(cell[0], cell[1]) if self.check_win(): self.win = True def flag_cell(self, x, y): if not self.revealed[x][y]: self.flagged[x][y] = not self.flagged[x][y] def check_win(self): for i in range(self.size): for j in range(self.size): if self.board[i][j] != -1 and not self.revealed[i][j]: return False return True def print_board(self): print(" ", end="") for i in range(self.size): print(i, end=" ") print() for i in range(self.size): print(i, end="") for j in range(self.size): if self.flagged[i][j]: print("F", end=" ") elif not self.revealed[i][j]: print(".", end=" ") elif self.board[i][j] == -1: print("*", end=" ") else: print(self.board[i][j], end=" ") print() def play(self): x = random.randint(0, self.size-1) y = random.randint(0, self.size-1) self.generate_board(x, y) while not self.gameover and not self.win: self.print_board() command = input("Enter command (R/F x y): ") parts = command.split() if len(parts) != 3: continue try: x = int(parts[1]) y = int(parts[2]) except ValueError: continue if x < 0 or x >= self.size or y < 0 or y >= self.size: continue if parts[0].lower() == "r": self.reveal_cell(x, y) elif parts[0].lower() == "f": self.flag_cell(x, y) self.print_board() if self.gameover: print("Game over!") elif self.win: print("You win!") minesweeper = Minesweeper() minesweeper.play() ``` 这个程序将生成一个随机的扫雷棋盘,并让玩家尝试找到所有的雷。玩家可以通过输入命令来揭示或标记单元格。祝你好运!

相关推荐

最新推荐

recommend-type

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a
recommend-type

matlab建立计算力学课程的笔记和文件.zip

matlab建立计算力学课程的笔记和文件.zip
recommend-type

FT-Prog-v3.12.38.643-FTD USB 工作模式设定及eprom读写

FT_Prog_v3.12.38.643--FTD USB 工作模式设定及eprom读写
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这