好玩的编程代码

时间:2025-03-08 21:06:14 手机游戏

猜数字游戏(Python)

```python

import random

def number_guessing_game():

print("欢迎来到神秘数字世界!")

print("我已经想好了一个1-100之间的数字,请你猜猜看~")

secret_number = random.randint(1, 100)

attempts = 0

max_attempts = 7

while attempts < max_attempts:

guess = int(input("猜一个数字:"))

attempts += 1

if guess > secret_number:

print(f"猜大了!还剩{max_attempts - attempts}次机会")

elif guess < secret_number:

print(f"小了小了,加油!")

else:

print(f"恭喜你,答对啦!用了{attempts}次就猜到了~")

break

number_guessing_game()

```

反转字符串(JavaScript)

```javascript

function reverseString(str) {

return str.split("").reverse().join("");

}

console.log(reverseString("Hello, world!"));

```

代码雨

安装 `cmatrix`:

```bash

sudo apt-get install cmatrix

```

运行 `cmatrix`,将会显示炫酷的代码雨效果。

跳跃的火

安装 `libaa-bin`:

```bash

sudo apt-get install libaa-bin

```

运行 `aafire`,将会显示跳跃的火焰效果。

幽灵打字

安装 `pv`:

```bash

sudo apt-get install pv

```

运行以下命令:

```bash

echo "For those who can’t hear music, people who dance are crazy" | pv -qL 10

```

程序员的表白

```python

print('\n'.join([' '.join([('LoveZYF' if ((x - y) % 8) else ' ') for x in range(-30, 30)]) for y in range(15, -15, -1)]))

```

乘法表

```python

print('\n'.join([' '.join([str(i * j) for j in range(1, 11)]) for i in range(1, 11)]))

```

生成包含错误的句子

```python

import random

def generate_sentence():

sentences = [

"我喜欢编程",

"Python是一门很棒的语言",

"编程能提高解决问题的能力",

"学习Python会让你事半功倍",

"数据结构与算法很有趣"

]

sentence = random.choice(sentences)

index = random.randint(0, len(sentence) - 1)

incorrect_char = sentence[index]

corrected_char = get_incorrect_char(incorrect_char)

incorrect_sentence = list(sentence)

incorrect_sentence[index] = corrected_char

return ' '.join(incorrect_sentence)

def get_incorrect_char(char):

这里可以定义一个简单的字符替换逻辑

incorrect_chars = {

'a': '4',

'e': '3',

'i': '1',

'o': '0',

's': '5'

}

return incorrect_chars.get(char, char)

print(generate_sentence())

```

这些代码示例涵盖了从简单的猜数字游戏到复杂的文本生成和视觉效果,适合不同编程语言和技能水平的开发者。希望这些示例能激发你的编程兴趣和创造力!