Problems when trying to create a QR code encoder

Question:

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" filesn--> ') )
>   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}")
Asked By: Luis Gomes

||

Answers:

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.

Answered By: fireshadow52
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.