Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Eggy-Chat-Bot-/whether.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
77 lines (62 sloc)
3.26 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import re | |
import json | |
from urllib.request import urlopen | |
user_input=input("for whether conditions using your ip address input 1 otherwise for whether by city name input 2:\n") | |
def whether_1(user_descition): | |
if user_input == "1": | |
url = 'http://ipinfo.io/json' | |
response = urlopen(url) | |
data = json.load(response) | |
city_1 = data['city'] | |
print("you live in "+city_1) | |
api_address = "https://api.openweathermap.org/data/2.5/weather?appid=499bb12a435d4b2ee90d62df4229d707&q=" | |
full_link = api_address + city_1 | |
json_data = requests.get(full_link).json() | |
temp_k = json_data["main"]["temp"] | |
situation_c = json_data["weather"][0]['main'] | |
float(temp_k) | |
temp_c1 = round(temp_k - 273, 1) | |
temp_c2= str(round(temp_k - 273, 1))+"C" | |
if temp_c1<0: | |
temp_c=temp_c2+" \nits freezing out there Wear a coat! " | |
elif temp_c1<=15: | |
temp_c=temp_c2+" \nits cold out there Wear a jacket! " | |
elif temp_c1<=25: | |
temp_c=temp_c2+" \nits moderate today a shirt would be fine! " | |
else: | |
temp_c=temp_c2+" \nits hot outside please wear a t-shirt! " | |
humidity = str(json_data["main"]["humidity"]) | |
final_statment_1="the situation is " + situation_c + " today in " + city_1 + " with a humidity of "+humidity +" and a temperature of " + temp_c | |
return final_statment_1 | |
elif user_input == "2": | |
while True: | |
user_location = input("please input your city to find latest whether conditions:\n") | |
api_address = "https://api.openweathermap.org/data/2.5/weather?appid=cdbc612d816811de2f71839e092ee261&q=" | |
full_link = api_address + user_location | |
json_data = requests.get(full_link).json() | |
code_1 = json_data["cod"] | |
if code_1 == 200: | |
temp_k = json_data["main"]["temp"] | |
situation_c = json_data["weather"][0]['main'] | |
float(temp_k) | |
temp_c1 = round(temp_k - 273, 1) | |
temp_c2 = str(round(temp_k - 273, 1)) + "C" | |
if temp_c1 < 0: | |
temp_c = temp_c2 + " \nits freezing out there Wear a coat! " | |
elif temp_c1 <= 15: | |
temp_c = temp_c2 + " \nits cold out there Wear a jacket! " | |
elif temp_c1 <= 25: | |
temp_c = temp_c2 + " \nits moderate today a shirt would be fine! " | |
else: | |
temp_c = temp_c2 + " \nits hot outside please wear a t-shirt! " | |
humidity = str(json_data["main"]["humidity"]) | |
code_2 = str(json_data["cod"]) | |
final_statment_2= "the situation is " + situation_c + " today in " + user_location + " with a humidity of "+humidity +" and a temperature of " + temp_c | |
return final_statment_2 | |
else: | |
print("sorry city not available please try again") | |
continue | |
else: | |
return "please choose one of the options available" | |
print(whether_1(user_input)) |