0

I'm trying to detect if http:// or https:// is in a variable string or not.
For example: http://google.com or https://google.com
While technically my code works, since it's all one word, it actually bypasses my code thinking that http:// or https:// isn't present in the string.

Is there a way to single out part of the string e.g., find http:// or https:// in a one-word string?

Code is attached - barcode_info currently contains "http://google.com" so it should bypass this part since the code is seeing if http:// or https:// is NOT contained in the string.

if "http://" or "https://" not in barcode_info:
    print("This QR Code doesn't contain a URL link. Would you still like to view the QR contents? \n")
    qr_Opt = input("1. Yes \n 2. No \n")
    if qr_Opt == "1":
        print("How would you like to view QR contents? \n")
        view_Opt = input("1. View in CLI \n2. Output to txt file \n3. View in CLI + txt file\n")
    if view_Opt == "1":
        print(barcode_info)
        quit()
    elif view_Opt == "2":
        file.write(barcode_info)
        quit()
    elif view_Opt == "3":
        print(barcode_info)
        file.write(barcode_info)
        quit()
    else:
        print("Invalid option. Quitting.")
        quit()
mousetail
  • 7,009
  • 4
  • 25
  • 45
ImKeelan
  • 25
  • 5
  • I think `if "http://" or "https://" not in barcode_info:` should be `if "http:// not in barcode_info and "https://" not in barcode_info`. The way you have it, with `"http://" or "https://"`, the `or` statement will return `"http://"`, so your statement is equivalent to `if "http://" not in barcode_info:`, I believe. – Random Davis Mar 02 '22 at 18:52
  • 2
    That's not how `or` works: `if "http://" or "https://" not in barcode_info` is always `True` because `"http://"` is truthy. – Timus Mar 02 '22 at 18:52
  • Thanks for your comments. I've changed "or" to "and" but still get the same issue. – ImKeelan Mar 02 '22 at 18:54
  • 1
    "I've changed "or" to "and" but still get the same issue." Please re-read the comment more carefully. You must change more than that. – Karl Knechtel Mar 02 '22 at 18:54

0 Answers0