I created an instance of Wetter
class and passed a number to the constructor. Variable zip
stays empty. Why?
This is the value of url2 at the moment: "https://api.openweathermap.org/data/2.5/weather?zip=,DE&units=metric&appid=xxx"
zip=
has no value.
I tried deleting the variable from the class, because I declared the variable as empty.
import requests
import json
class Wetter:
key = "xxx" # API KEY von openweathermaps eintragen
url = "https://api.openweathermap.org/data/2.5/weather?zip="
parameters = ",DE&units=metric&appid=" + str(key)
zip = ""
url2 = "https://api.openweathermap.org/data/2.5/weather?zip=" + str(zip) + ",DE&units=metric&appid=" + str(key)
data = ""
json = ""
def __init__(this, zip):
this.zip = zip
def printWeather(this):
this.data = requests.get(this.url2)
this.json = json.loads(this.data.text)
print("Stadt: " + str(this.json["name"]) + "\nTemperatur: " + str(this.json["main"]["temp"]))
if __name__ == "__main__":
zip = str(input("PLZ eingeben: "))
obj = Wetter(zip)
obj.printWeather()
I expect the variable to be filled with the delivered zip value.
You need to update url2
after this.zip
was changed:
def __init__(this, zip):
this.zip = zip
this.url2 = "https://api.openweathermap.org/data/2.5/weather?zip=" + str(this.zip) + ",DE&units=metric&appid=" + str(this.key)
It's a simple mistake Instead of doing
zip = ""
Try doing
this.zip = ""
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.