python有趣代码实例
时间: 2023-12-12 16:35:52 浏览: 76
以下是两个有趣的Python代码实例:
1. 猜数字游戏
```python
import random
number = random.randint(1, 100)
guess = int(input("Guess a number between 1 and 100: "))
while guess != number:
if guess < number:
print("Too low")
else:
print("Too high")
guess = int(input("Guess again: "))
print("You guessed it!")
```
2. 烟花秀
```python
import tkinter as tk
import random
# 创建画布
canvas_width = 500
canvas_height = 500
canvas = tk.Canvas(width=canvas_width, height=canvas_height)
canvas.pack()
# 创建烟花
def create_firework():
x = random.randint(0, canvas_width)
y = random.randint(0, canvas_height)
canvas.create_oval(x-5, y-5, x+5, y+5, fill="red", outline="")
# 每隔一段时间创建一个烟花
def explode():
create_firework()
canvas.after(100, explode)
# 启动烟花秀
explode()
```
阅读全文