You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用check50测试Python二维码项目时遇退出码错误:预期0,实际1

期末Python二维码生成项目check50测试失败问题解决

问题现象

运行check50测试时出现错误:

:( main function exists expected exit code 0, not 1

错误日志详情:

:( main function exists
Cause
expected exit code 0, not 1
Log
running python3 custom_checks.py main_function...
checking that program exited with status 0...

项目代码(qr_generator.py)

import qrcode
from PIL import Image, ImageDraw, ImageOps
import time

def generate_qr_code(website_name):
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=10,
        border=4,
    )

    qr.add_data(website_name)
    qr.make(fit=True)

    # Create a QR code image
    qr_img = qr.make_image(fill_color="black", back_color="white")

    return qr_img

def apply_texture_to_qr_code(qr_img, texture):
    # Resize the texture to fit the QR code size
    texture = ImageOps.fit(texture, qr_img.size, Image.ANTIALIAS)

    # Apply the texture to the QR code
    qr_img = Image.alpha_composite(qr_img.convert("RGBA"), texture)

    return qr_img

def generate_custom_qr_code(website_name, background_color="#FFFFFF", texture_path=None):
    qr_code = generate_qr_code(website_name)

    if texture_path:
        # If a texture path is provided, apply it to the QR code background
        texture = Image.open(texture_path)
        qr_code = apply_texture_to_qr_code(qr_code, texture)

    # Create the final custom QR code with the specified background color
    custom_qr_code = Image.new("RGB", qr_code.size, background_color)
    custom_qr_code.paste(qr_code, (0, 0), qr_code)

    return custom_qr_code

def save_qr_code(qr_code, file_name):
    qr_code.save(file_name)

if __name__ == "__main__":
    website_name = input("Enter the website name: ")
    background_color = input("Enter the background color (e.g., #FF0000 for red): ")


    # Optional: Provide a texture path to apply a texture to the QR code background
    texture_path = ""  # Set the path to the texture image if needed

    qr_code = generate_custom_qr_code(website_name, background_color, texture_path)

    # Generate a unique file name based on the current timestamp
    timestamp = int(time.time())
    file_name = f"custom_qr_code_{timestamp}.png"

    save_qr_code(qr_code, file_name)

测试代码

import unittest
from PIL import Image
from qr_generator import generate_qr_code, apply_texture_to_qr_code, generate_custom_qr_code

class TestQRCodeGenerator(unittest.TestCase):
    def test_generate_qr_code(self):
        website_name = "www.example.com"
        qr_code = generate_qr_code(website_name)
        self.assertIsNotNone(qr_code)
        # Add more specific assertions if needed

    def test_apply_texture_to_qr_code(self):
        website_name = "www.example.com"  # Provide a website name
        qr_code = generate_qr_code(website_name)
        texture_path = None  # Set to the texture image path if needed
        texture = Image.open(texture_path) if texture_path else None
        qr_code_with_texture = apply_texture_to_qr_code(qr_code, None)
        self.assertIsNotNone(qr_code_with_texture)
        Add more specific assertions if needed

    def test_generate_custom_qr_code(self):
        website_name = "www.example.com"  # Provide a website name
        background_color = "#FFFFFF"
        texture_path = None  # Set to the texture image path if needed
        custom_qr_code = generate_custom_qr_code(website_name, background_color, texture_path)
        self.assertIsNotNone(custom_qr_code)
        

if __name__ == "__main__":
    unittest.main()

问题原因

  1. 主函数依赖交互式输入:check50测试为非交互式环境,无法响应input()调用,导致程序阻塞或异常退出,返回码为1。
  2. 测试文件语法错误:test_apply_texture_to_qr_code方法中Add more specific assertions if needed未加注释,属于语法错误,会导致测试运行失败。
  3. 空路径处理逻辑错误:主函数中texture_path设为空字符串,generate_custom_qr_code里if texture_path:会将空字符串判定为真值,尝试打开不存在的文件,触发FileNotFoundError导致程序崩溃。

修复方案

1. 适配主函数的非交互式运行

修改主函数,同时支持命令行参数和交互式输入,避免测试时阻塞:

if __name__ == "__main__":
    import sys
    # 优先读取命令行参数,适配非交互式测试
    if len(sys.argv) >= 3:
        website_name = sys.argv[1]
        background_color = sys.argv[2]
        texture_path = sys.argv[3] if len(sys.argv) > 3 else None
    else:
        # 保留交互式输入作为备用
        website_name = input("Enter the website name: ") or "www.example.com"
        background_color = input("Enter the background color (e.g., #FF0000 for red): ") or "#FFFFFF"
        texture_path = input("Enter texture path (optional): ") or None

    qr_code = generate_custom_qr_code(website_name, background_color, texture_path)

    timestamp = int(time.time())
    file_name = f"custom_qr_code_{timestamp}.png"

    save_qr_code(qr_code, file_name)

2. 修复测试文件的语法与逻辑错误

import unittest
from PIL import Image
from qr_generator import generate_qr_code, apply_texture_to_qr_code, generate_custom_qr_code

class TestQRCodeGenerator(unittest.TestCase):
    def test_generate_qr_code(self):
        website_name = "www.example.com"
        qr_code = generate_qr_code(website_name)
        self.assertIsNotNone(qr_code)
        # Add more specific assertions if needed

    def test_apply_texture_to_qr_code(self):
        website_name = "www.example.com"
        qr_code = generate_qr_code(website_name)
        # 创建临时纹理图片用于测试,避免传递None触发错误
        texture = Image.new("RGBA", qr_code.size, (255, 255, 255, 128))
        qr_code_with_texture = apply_texture_to_qr_code(qr_code, texture)
        self.assertIsNotNone(qr_code_with_texture)
        # Add more specific assertions if needed

    def test_generate_custom_qr_code(self):
        website_name = "www.example.com"
        background_color = "#FFFFFF"
        texture_path = None
        custom_qr_code = generate_custom_qr_code(website_name, background_color, texture_path)
        self.assertIsNotNone(custom_qr_code)
        

if __name__ == "__main__":
    unittest.main()

3. 修正纹理路径判断逻辑

确保空字符串被视为无效路径:

def generate_custom_qr_code(website_name, background_color="#FFFFFF", texture_path=None):
    qr_code = generate_qr_code(website_name)

    # 仅当路径非空且不为空白字符时加载纹理
    if texture_path and texture_path.strip():
        texture = Image.open(texture_path)
        qr_code = apply_texture_to_qr_code(qr_code, texture)

    custom_qr_code = Image.new("RGB", qr_code.size, background_color)
    custom_qr_code.paste(qr_code, (0, 0), qr_code)

    return custom_qr_code

内容的提问来源于stack exchange,提问作者Rythme Nagrani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 02:12:39