MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / get_barcode

Function get_barcode

strings/barcode_validator.py:56–73  ·  view source on GitHub ↗

Returns the barcode as an integer >>> get_barcode("8718452538119") 8718452538119 >>> get_barcode("dwefgiweuf") Traceback (most recent call last): ... ValueError: Barcode 'dwefgiweuf' has alphabetic characters.

(barcode: str)

Source from the content-addressed store, hash-verified

54
55
56def get_barcode(barcode: str) -> int:
57 """
58 Returns the barcode as an integer
59
60 >>> get_barcode("8718452538119")
61 8718452538119
62 >>> get_barcode("dwefgiweuf")
63 Traceback (most recent call last):
64 ...
65 ValueError: Barcode 'dwefgiweuf' has alphabetic characters.
66 """
67 if str(barcode).isalpha():
68 msg = f"Barcode '{barcode}' has alphabetic characters."
69 raise ValueError(msg)
70 elif int(barcode) < 0:
71 raise ValueError("The entered barcode has a negative value. Try again.")
72 else:
73 return int(barcode)
74
75
76if __name__ == "__main__":

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected