tuomasvaltanen

Untitled

Nov 8th, 2021 (edited)
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.83 KB | None | 0 0
  1. print("Tervetuloa! Tänään monimutkaiset datarakenteet!")
  2.  
  3. # kolmen päivän lämpötilat, lista mikä koostuu listoista (temperatures)
  4. day_1 = [-11.2, -5.7, -7.8, -8.6]
  5. day_2 = [0.6, 2.3, 3.7, 1.4]
  6. day_3 = [3.6, 4.5, 2.7, 3.9]
  7.  
  8. temperatures = [day_1, day_2, day_3]
  9.  
  10. # print(temperatures)
  11.  
  12. for d in temperatures:
  13.     print("Uusi päivä!")
  14.    
  15.     for temp in d:
  16.         print(temp)
  17.  
  18. # UUSI TIEDOSTO
  19.  
  20. # dictionary dictionaryn sisällä
  21. book = {
  22.     "name":  "My Lady Jane",
  23.     "year":  2016,
  24.     "publisher": {
  25.         "name": "HarperTeen",
  26.         "organization": "HarperCollins Publishers",
  27.         "location": "New York"
  28.     }
  29. }
  30.  
  31. # kirjan nimi
  32. book_name = book['name']
  33. print(book_name)
  34.  
  35. # nopea tapa, mutta ei ehkä niin selkeä
  36. publisher_name = book['publisher']['name']
  37. print(publisher_name)
  38.  
  39. # helpompi tapa, välivaiheilla
  40. publisher = book['publisher']
  41. publisher_name = publisher['name']
  42. print(publisher_name)
  43.  
  44. # UUSI TIEDOSTO
  45.  
  46. # lista missä on dictionaryjä, eli lista tuotteita!
  47. products = [
  48.     {"name": "Kahvinkeitin", "price": 129},
  49.     {"name": "Astianpesukone", "price": 299},
  50.     {"name": "Arkkupakastin", "price": 199}
  51. ]
  52.  
  53. # silmukoidaan tuotteet läpi
  54. for p in products:
  55.     print(p["name"])
  56.     print(f"Hinta: {p['price']}€")
  57.     print()
  58.  
  59. # UUSI TIEDOSTO, HOTELLIT
  60.  
  61. # esimerkki 1
  62.  
  63. import var_dump as vd
  64.  
  65. # luodaan hotelli no. 1
  66. hotel_1 = {
  67.     "name": "Snow Line Hotels",
  68.     "rating": 4.3,
  69.     "wifi": True,
  70.     "free_breakfast": True,
  71.     "services": ["sauna", "meetings", "restaurant", "parking", "safaris"],
  72.     "price_level": 4
  73. }
  74.  
  75. # luodaan hotelli no. 2
  76. hotel_2 = {
  77.     "name": "North Ice Hostel",
  78.     "rating": 3.5,
  79.     "wifi": True,
  80.     "free_breakfast": False,
  81.     "services": ["sauna", "parking"],
  82.     "price_level": 2
  83. }
  84.  
  85. # asetetaan molemmat hotellit samaan listaan
  86. hotels = [hotel_1, hotel_2]
  87. # print(hotels)
  88. # print()
  89. # vd.var_dump(hotels)
  90.  
  91. for hotel in hotels:
  92.     print(hotel['name'])
  93.  
  94.     # perinteinen tapa, sisäkkäiset for-silmukat
  95.     # for service in hotel['services']:
  96.     #     print(service)
  97.  
  98.     # tekstilistoilla voidaan käyttää myös joinia
  99.     # eli yhdistetään kaikki palvelut rivinvaihdolla ja tulostetaan!
  100.     services = "\n".join(hotel['services'])
  101.     print(services)
  102.  
  103.     print()
  104.  
  105. # VERSIO 2, suodatuksia
  106.  
  107. sauna_hotels = []
  108.  
  109. for hotel in hotels:
  110.     print(hotel['name'])
  111.  
  112.     # for service in hotel['services']:
  113.     #     if service == 'sauna':
  114.     #         print("Sauna löytyy!")
  115.     #
  116.     #     if service == 'restaurant':
  117.     #         print("Hotellissa on ravintola.")
  118.  
  119.     if "sauna" in hotel['services']:
  120.         sauna_hotels.append(hotel['name'])
  121.  
  122.     print()
  123.  
  124. print(sauna_hotels)
  125.  
  126. # UUSI TIEDOSTO, EVENTS RAJAPINTA
  127. import urllib.request
  128. import json
  129.  
  130. # this module needs to be installed separately
  131. # in PyCharm you can install the package if its not found!
  132. import var_dump as vd
  133.  
  134. # get internet data
  135. url = 'https://edu.frostbit.fi/api/events'
  136. req = urllib.request.Request(url)
  137. raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
  138.  
  139. # tarvittava data on nyt data-muuttujassa!
  140. data = json.loads(raw_data)
  141. # vd.var_dump(data)
  142.  
  143. choice = input("Millaisia tapahtumia haet?\n")
  144.  
  145. for event in data:
  146.     # haetaan osoitetiedot, tulostetaan postinumero + osoite
  147.     address = event['address']
  148.     address_text = f"{address['postal_code']} {address['street_address']}"
  149.  
  150.     # kategorialista siistiksi pilkulla erotelluksi tekstiksi
  151.     categories = ", ".join(event['categories'])
  152.  
  153.     if choice not in categories:
  154.         continue
  155.  
  156.     print(event['name'])
  157.     print(address_text)
  158.  
  159.     # jos ei ole kategorioita, tulostetaan käyttäjälle viesti
  160.     # muutoin tulostetaan kategoriat
  161.     if categories != "":
  162.         print(categories)
  163.     else:
  164.         print("Ei kategorioita.")
  165.  
  166.     print()
Add Comment
Please, Sign In to add comment