[英]Python Value passed to constructor, Variable still empty
我創建了Wetter
類的實例,並向構造函數傳遞了一個數字。 可變的zip
保持為空。 為什么?
這是目前url2的值: "https://api.openweathermap.org/data/2.5/weather?zip=,DE&units=metric&appid=xxx"
zip=
沒有價值。
我嘗試從類中刪除變量,因為我將變量聲明為空。
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()
我希望變量將用交付的zip值填充。
您需要更新url2
后this.zip
改為:
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)
這是一個簡單的錯誤
zip = ""
嘗試做
this.zip = ""
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.