pycharm有趣代码
时间: 2023-10-15 22:05:07 浏览: 68
当然有!以下是一些有趣的代码片段,你可以在PyCharm中运行它们:
1. 打印斐波那契数列:
```python
def fibonacci(n):
if n <= 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
else:
sequence = [0, 1]
while len(sequence) < n:
next_num = sequence[-1] + sequence[-2]
sequence.append(next_num)
return sequence
n = int(input("请输入要打印的斐波那契数列的个数:"))
print(fibonacci(n))
```
2. 猜数字游戏:
```python
import random
number = random.randint(1, 100)
guess = None
while guess != number:
guess = int(input("猜一个1到100之间的数字: "))
if guess < number:
print("猜小了")
elif guess > number:
print("猜大了")
else:
print("恭喜,猜对了!")
```
3. 绘制彩色螺旋图案:
```python
import turtle
colors = ["red", "orange", "yellow", "green", "blue", "purple"]
turtle.speed(0)
for x in range(360):
turtle.pencolor(colors[x % len(colors)])
turtle.width(x / 100 + 1)
turtle.forward(x)
turtle.left(59)
turtle.done()
```
这些代码片段可以让你在PyCharm中获得一些有趣的体验。尽情享受编程的乐趣吧!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)