Search code examples
pythonunit-testingcs50

while running check50 on my final project i am getting :( main function exists expected exit code 0, not 1 error


this is the log details of the error:

:( 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...

the project file in which the main functions exists is:

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)

and the test file is

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()

I was expecting to pass all check50 testcases but 1 was failed.


Solution

  • As noted, you don't have a main() function defined. This is what I think check50 expects to find:

    def main():
        #move  your "main" code here
    
    if __name__ == "__main__":
        main()