Python的有趣代码
时间: 2024-03-30 14:32:24 浏览: 64
python爱心代码.rar
当然!Python有很多有趣的代码,以下是其中一些例子:
1. 猜数字游戏:
```python
import random
number = random.randint(1, 100)
guess = int(input("猜一个1到100之间的数字:"))
while guess != number:
if guess < number:
print("猜小了!")
else:
print("猜大了!")
guess = int(input("再猜一次:"))
print("恭喜你,猜对了!")
```
2. 打印九九乘法表:
```python
for i in range(1, 10):
for j in range(1, i+1):
print(f"{j} * {i} = {i*j}", end="\t")
print()
```
3. 绘制彩色螺旋图案:
```python
import turtle
colors = ["red", "purple", "blue", "green", "yellow", "orange"]
turtle.bgcolor("black")
turtle.speed(0)
for x in range(360):
turtle.pencolor(colors[x % 6])
turtle.width(x / 100 + 1)
turtle.forward(x)
turtle.left(59)
```
希望你喜欢这些有趣的Python代码!
阅读全文