传递给构造函数的Python值,变量仍然为空
我创建了一个传递给构造函数的Python值,变量仍然为空,python,class,Python,Class,我创建了一个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 openwe
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值填充变量。这是一个简单的错误
而不是做
zip = ""
试着做
this.zip = ""
更改
此.zip
后,您需要更新url2
:
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)
但是在哪里呢?在哪一行?在顶级声明中