编写两个有意思的Python
时间: 2024-09-30 19:04:17 浏览: 34
有意思的Python小程序
编写两个有意思的Python程序示例:
1. **猜数字游戏**:
```python
import random
def guess_number():
number_to_guess = random.randint(1, 100)
attempts = 0
print("欢迎参加猜数字游戏!我想了一个1到100之间的整数,你有5次机会猜测。")
while True:
user_guess = int(input("请输入你的猜测:"))
attempts += 1
if user_guess < number_to_guess:
print("太小了!再试一次。")
elif user_guess > number_to_guess:
print("太大了!再试一次。")
else:
print(f"恭喜你,猜对了!你总共用了{attempts}次尝试。")
break
guess_number()
```
2. **简单的文本生成器 - 短信祝福语**:
```python
import random
greetings = ["早上好", "嗨", "祝你有个美好的一天", "加油", "晚安"]
phrases = ["记得开心", "保持微笑", "工作顺利", "好好休息"]
def generate_text(name):
return f"{random.choice(greetings)} {name}, {random.choice(phrases)}!"
recipient_name = input("请输入收件人的名字:")
print(generate_text(recipient_name))
#
阅读全文