[BUG] Code cannot be displayed correctly #1684

Closed
opened 2025-11-11 14:50:04 -06:00 by GiteaMirror · 2 comments
Owner

Originally created by @Yanyutin753 on GitHub (Aug 4, 2024).

Bug Report

Description

Code cannot be displayed correctly

image

Message

https://openwebui.com/c/yangyutin753/d9a81ae1-ad60-4ec9-bb74-1184ef2fe5fa

学习编程的最佳方式之一是通过实践项目。以下是几个适合初学者的Python项目,每个项目都附有简要介绍:

1. **猜数字游戏(Number Guessing Game)**
   - **简介**:用户输入一个数字,程序生成一个随机数,用户需要猜出这个数字,程序会提示猜大了还是猜小了。
   - **目标**:熟悉基本的输入输出、循环、条件语句、随机数生成等基础知识。
   - **扩展**:增加猜测次数限制,加入得分系统,添加难度选择。

2. **记账应用(Expense Tracker)**
   - **简介**:用户可以输入每日支出,程序可以记录支出并生成支出报告。
   - **目标**:学习如何处理用户输入、读写文件、数据存储和简单的数据处理。
   - **扩展**:加入图表显示支出情况,添加不同类别的支出统计,开发一个简单的用户界面。

3. **密码生成器(Password Generator)**
   - **简介**:根据用户设定的规则(如长度、是否包含特殊字符等)生成随机密码。
   - **目标**:理解字符串操作、随机数生成、函数定义和调用等。
   - **扩展**:添加密码强度检查,保存生成的密码,制作一个GUI界面。

4. **网站爬虫(Web Scraper)**
   - **简介**:从某个网站抓取特定信息并保存到本地。
   - **目标**:学习网络请求、HTML解析、数据处理和存储。
   - **扩展**:爬取多个页面,处理动态内容,学习反爬机制。

5. **待办事项列表(To-Do List)**
   - **简介**:一个简单的应用,允许用户添加、删除和查看待办事项。
   - **目标**:掌握数据结构(如列表)、文件操作、简单的CRUD(创建、读取、更新、删除)操作。
   - **扩展**:加入优先级设置、截止日期提醒,开发一个Web版本。

### 项目细节
1. 猜数字游戏
   ```python
   import random

   def guess_number():
       number = random.randint(1, 100)
       tries = 0
       while True:
           guess = int(input("猜一个数字: "))
           tries += 1
           if guess < number:
               print("太小了!")
           elif guess > number:
               print("太大了!")
           else:
               print(f"恭喜你,猜对了!一共猜了 {tries} 次。")
               break

   guess_number()
  1. 记账应用

    def add_expense():
        date = input("请输入日期 (YYYY-MM-DD): ")
        amount = float(input("请输入支出金额: "))
        category = input("请输入支出类别: ")
        with open("expenses.txt", "a") as file:
            file.write(f"{date},{amount},{category}\n")
        print("记录已保存!")
    
    def view_expenses():
        with open("expenses.txt", "r") as file:
            for line in file:
                print(line.strip())
    
    add_expense()
    view_expenses()
    
  2. 密码生成器

    import random
    import string
    
    def generate_password(length=12):
        characters = string.ascii_letters + string.digits + string.punctuation
        password = ''.join(random.choice(characters) for i in range(length))
        return password
    
    print("生成的密码:", generate_password())
    
  3. 网站爬虫

    import requests
    from bs4 import BeautifulSoup
    
    def scrape_website(url):
        response = requests.get(url)
        soup = BeautifulSoup(response.text, 'html.parser')
        for item in soup.find_all('h2'):
            print(item.get_text())
    
    scrape_website('https://example.com')
    
  4. 待办事项列表

    tasks = []
    
    def add_task():
        task = input("请输入任务: ")
        tasks.append(task)
        print("任务已添加!")
    
    def view_tasks():
        print("待办事项:")
        for i, task in enumerate(tasks, 1):
            print(f"{i}. {task}")
    
    add_task()
    view_tasks()
    
这些项目不仅能帮助你熟悉Python的基本语法和功能,还能逐步增强你的编程能力。你可以根据自己的兴趣和进度不断扩展这些项目,挑战更复杂的功能。
Originally created by @Yanyutin753 on GitHub (Aug 4, 2024). # Bug Report ## Description ### Code cannot be displayed correctly ![image](https://github.com/user-attachments/assets/4777292f-c77f-468d-84f0-249e0b8b7830) Message https://openwebui.com/c/yangyutin753/d9a81ae1-ad60-4ec9-bb74-1184ef2fe5fa ``` 学习编程的最佳方式之一是通过实践项目。以下是几个适合初学者的Python项目,每个项目都附有简要介绍: 1. **猜数字游戏(Number Guessing Game)** - **简介**:用户输入一个数字,程序生成一个随机数,用户需要猜出这个数字,程序会提示猜大了还是猜小了。 - **目标**:熟悉基本的输入输出、循环、条件语句、随机数生成等基础知识。 - **扩展**:增加猜测次数限制,加入得分系统,添加难度选择。 2. **记账应用(Expense Tracker)** - **简介**:用户可以输入每日支出,程序可以记录支出并生成支出报告。 - **目标**:学习如何处理用户输入、读写文件、数据存储和简单的数据处理。 - **扩展**:加入图表显示支出情况,添加不同类别的支出统计,开发一个简单的用户界面。 3. **密码生成器(Password Generator)** - **简介**:根据用户设定的规则(如长度、是否包含特殊字符等)生成随机密码。 - **目标**:理解字符串操作、随机数生成、函数定义和调用等。 - **扩展**:添加密码强度检查,保存生成的密码,制作一个GUI界面。 4. **网站爬虫(Web Scraper)** - **简介**:从某个网站抓取特定信息并保存到本地。 - **目标**:学习网络请求、HTML解析、数据处理和存储。 - **扩展**:爬取多个页面,处理动态内容,学习反爬机制。 5. **待办事项列表(To-Do List)** - **简介**:一个简单的应用,允许用户添加、删除和查看待办事项。 - **目标**:掌握数据结构(如列表)、文件操作、简单的CRUD(创建、读取、更新、删除)操作。 - **扩展**:加入优先级设置、截止日期提醒,开发一个Web版本。 ### 项目细节 1. 猜数字游戏 ```python import random def guess_number(): number = random.randint(1, 100) tries = 0 while True: guess = int(input("猜一个数字: ")) tries += 1 if guess < number: print("太小了!") elif guess > number: print("太大了!") else: print(f"恭喜你,猜对了!一共猜了 {tries} 次。") break guess_number() ``` 2. 记账应用 ```python def add_expense(): date = input("请输入日期 (YYYY-MM-DD): ") amount = float(input("请输入支出金额: ")) category = input("请输入支出类别: ") with open("expenses.txt", "a") as file: file.write(f"{date},{amount},{category}\n") print("记录已保存!") def view_expenses(): with open("expenses.txt", "r") as file: for line in file: print(line.strip()) add_expense() view_expenses() ``` 3. **密码生成器** ```python import random import string def generate_password(length=12): characters = string.ascii_letters + string.digits + string.punctuation password = ''.join(random.choice(characters) for i in range(length)) return password print("生成的密码:", generate_password()) ``` 4. **网站爬虫** ```python import requests from bs4 import BeautifulSoup def scrape_website(url): response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') for item in soup.find_all('h2'): print(item.get_text()) scrape_website('https://example.com') ``` 5. **待办事项列表** ```python tasks = [] def add_task(): task = input("请输入任务: ") tasks.append(task) print("任务已添加!") def view_tasks(): print("待办事项:") for i, task in enumerate(tasks, 1): print(f"{i}. {task}") add_task() view_tasks() ``` ``` 这些项目不仅能帮助你熟悉Python的基本语法和功能,还能逐步增强你的编程能力。你可以根据自己的兴趣和进度不断扩展这些项目,挑战更复杂的功能。
Author
Owner

@tjbck commented on GitHub (Aug 5, 2024):

Fixed on dev!

@tjbck commented on GitHub (Aug 5, 2024): Fixed on dev!
Author
Owner

@Yanyutin753 commented on GitHub (Aug 6, 2024):

Thanks!!!

@Yanyutin753 commented on GitHub (Aug 6, 2024): Thanks!!!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#1684