0% found this document useful (0 votes)
20 views14 pages

2 - Writing Python Programs Using Raspberry Pi

Uploaded by

Vinh Pham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views14 pages

2 - Writing Python Programs Using Raspberry Pi

Uploaded by

Vinh Pham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14



python

print 'hello'
python
python3

debug
python3

from datetime import datetime

datetime datetime

print(datetime.now())
ImportError pyjokes

sudo pip3 install pyjokes pyjokes


weather
pip
pip3
weather-api

python3

Weather Unit weather

weather weather

lookup 4118

condition
lookup
class CurrentWeather:
weather_data={'Toronto':['13','partly sunny','8 km/h NW'],
'Montreal':['16','mostly sunny','22 km/h W'],
'Vancouver':['18','thunder showers','10 km/h NE'],
'New York':['17','mostly cloudy','5 km/h SE'],
'Los Angeles':['28','sunny','4 km/h SW'],
'London':['12','mostly cloudy','8 km/h NW'],
'Mumbai':['33','humid and foggy','2 km/h S']
}

def __init__(self, city):


self.city = city

def getTemperature(self):
return self.weather_data[self.city][0]

def getWeatherConditions(self):
return self.weather_data[self.city][1]

def getWindSpeed(self):
return self.weather_data[self.city][2]
CurrentWeather

CurrentWeather London

londonWeather = CurrentWeather('London')

londonWeather
CurrentWeather 'London' init
London
self.city

weatherLondon.getTemperature()

'12'

London

weatherLondon.getWeatherConditions()

'mostly cloudy'

weatherLondon.getWindSpeed()

8 km/h NW
CurrentWeather
weather_data

londonWeather
londonWeather CurrentWeather
'London'
CurrentWeather

def getCity(self):
return self.city

CurrentWeather.py

if __name__ == "__main__":
currentWeather = CurrentWeather('Toronto')
wind_dir_str_len = 2

if currentWeather.getWindSpeed()[-2:-1] == ' ':


wind_dir_str_len = 1

print("The current temperature in",


currentWeather.getCity(),"is",
currentWeather.getTemperature(),
"degrees Celsius,",
"the weather conditions are",
currentWeather.getWeatherConditions(),
"and the wind is coming out of the",
currentWeather.getWindSpeed()[-(wind_dir_str_len):],
"direction with a speed of",
currentWeather.getWindSpeed()
[0:len(currentWeather.getWindSpeed())
-(wind_dir_str_len)]
)

The current temperature in Toronto is 13 degrees Celsius, the


weather conditions are partly sunny and the wind is coming out of
the NW direction with a speed of 8 km/h

if __name__ == "__main__":
if
CurrentWeather.py
if
wind_dir_str_len
S

wind_dir_str_len = 2
if currentWeather.getWindSpeed()[-2:-1] == ' ':
wind_dir_str_len = 1

[-2:-1]
1

getCity
currentWeather torontoWeather

currentWeather = CurrentWeather('Toronto')

currentWeather = CurrentWeather('Mumbai')

The current temperature in Mumbai is 33 degrees Celsius, the weather


conditions are humid and foggy and the wind is coming out of the S
direction with a speed of 2 km/h
python

python3

Chapter 3

getCity
CurrentWeather

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy