Home > Blockchain >  Problems when trying to create a QR code encoder
Problems when trying to create a QR code encoder

Time:09-04

I'm trying to create an encoder for QR Code so when I click to start it gives an error in this part (the part that is different from the rest and the part that is giving error)

import qrcode

link_text = str(input('Enter the website/text you want: '))

>   filename = str(input('Enter the filename: ')) image_type = str(input('Enter which image type: \n1 --> For "png" files \n2 --> For "jpeg" files \n3 --> For "jpg" files\n--> ') )
>   image_format = "" if image_type == 1:
>   image_format = "png" elif image_type == 2:
>   image_format = "jpeg" elif image_type == 3:
>   image_format = "jpg"

img = qrcode.make(link_text)
type(img)
img.save(f"{file_name}.{image_format}")

CodePudding user response:

image_type is a str, which you're trying compare with int literals (i.e. 1, 2, 3). You can't compare a str with an int; you'll always get False. Either cast image_type into an int or, better yet, scrap the image_type variable, allow the user to directly enter "PNG", "JPEG", or "JPG", and just use that as the extension.

  • Related