-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
35 lines (27 loc) · 890 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import requests
def consultar_cep(cep):
url = f"https://viacep.com.br/ws/{cep}/json/"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return data
else:
return None
def main():
while True:
try:
cep = int(input("Digite o CEP: "))
endereco = consultar_cep(cep)
try:
if len(str(cep)) != 8:
raise ValueError("O Cep tem 8 Digits")
else:
print(f"{endereco['logradouro']}, Bairro {endereco['bairro']}, {endereco['localidade']}/"
f"{endereco['uf']}, DDD:{endereco['ddd']}")
break
except ValueError as e:
print(e)
except ValueError:
print('Digite um cep válido')
if __name__ == "__main__":
main()