Python Forum

Full Version: How to use function result in another function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,

I would like to know how to use function result in another function.. I explain..
I created a function to retrieve weather data in a JSON file.

Here is the code from weather.py:
    def weather_request(self):
        self.api_link = "https://api.openweathermap.org/data/2.5/weather?q=castelnau-le-lez&lang=fr&appid=738e9d1489491771d579f8b5db5fd21a"
        self.json_data = requests.get(self.api_link).json()

        return self.json_data
So far, no problem, I know it works, but the wind speed is in m/s, I want it in km/s.
So I have to do a conversion, which I know, but the problem comes from the recovery of the value contained in the JSON file.

Here is the call code from update.py
# -- Python modules
import pygame as pg
import requests
# -- Personnal modules
from ui import constants as cst


class LSWind:

    def __init__(self, screen):
        self.screen = screen
        self.w_font = pg.font.SysFont("", 50)

    def wind_request(self, json_data):
        self.w_speed = json_data['wind']['speed']

        return self

    def wind_display(self, color):
        self.w_km = " Km/h"
        self.w_current_speed = int(self.wind_request() * 3600 / 1000)
        self.w_display = str(self.w_current_speed) + self.w_km

        self.screen.master.blit(cst.WIND_FLAG, (630, 318))
        self.show_w = self.w_font.render(self.w_display, True, color)
        self.center_y = (344 - (self.show_w.get_height() / 2) + 5)
        self.screen.master.blit(self.show_w, (700, self.center_y))

        return self.w_display
"def wind_request" call the json file from (weather.py) but I don't know what to use for convert wind speed.
If i call the function name (wind_request()), it ask me a parameter, of course and if I call the (self.w_speed) variable, here is the error message
Error:
self.w_current_speed = int(self.w_speed * 3600 / 1000) AttributeError: 'LSWind' object has no attribute 'w_speed'
Anyone have an idea please ? Thank you
You are on the right track with return functions.
def add_10(num):
    result = num + 10
    return result

total = add_10(5)
print(total, add_10(5))
This little example shows how you send info to one function and get it back in a variable that you can send to the next function if you want.
weather has not been imported into update
weather_request is a method of a class, you would need to make an instance of that class to call weather_request.
(although looking at it, there is nothing coming from elsewhere in the class)
The result of this you would pass as an attribute to the method wind_request
This will create the attribute self.w_speed (return self is not required)
then you can call self.w_current_speed = int(self.w_speed * 3600 / 1000)
There's nothing showing how you're running the code and the error message has different code than the code given. Going off the error code, it looks like wind_display is being run before win_request causing for that error to happen because the variable has yet to become an attribute of the function. Either you can run it wherever you're running this other code, or you can pass json_data to the display function and call the wind_request from wind_display. You can also call weather_request from weather_display to get the json_data and use that json_data for wind_request, though since it looks like that function comes from a different class you'd have to pass the class as an argument. Now based on the code given, if you want to call wind_request from inside wind_display then it'll need to return self.w_speed, then you'd also yer again need the json_data to be passed into wind_display.
Hope this helps
Thank you very much for all your replies..
I will try all of this.