Python 名称错误:名称';countrychoice&x27;没有定义
我正在写的程序有点问题 我让用户输入一个国家,然后输入该国的一个城市,然后使用API查看所选城市的天气预报 我正在使用一个类,如下所示:Python 名称错误:名称';countrychoice&x27;没有定义,python,list,python-3.x,nameerror,defined,Python,List,Python 3.x,Nameerror,Defined,我正在写的程序有点问题 我让用户输入一个国家,然后输入该国的一个城市,然后使用API查看所选城市的天气预报 我正在使用一个类,如下所示: class requestChoice: def __init__(self): self.countrychoice = None self.citychoice = None def countryChoice(self): self.countrychoice = input("Ente
class requestChoice:
def __init__(self):
self.countrychoice = None
self.citychoice = None
def countryChoice(self):
self.countrychoice = input("Enter which country your city is in(in english): ")
def cityChoice(self):
self.citychoice = input("Enter the name of the city: ")
from requestchoice import requestChoice
import requests
if __name__ == '__main__':
"""Introducion"""
print ("\nThis program lets you see a weather forecast for your choosen city.")
rc = requestChoice()
while True:
print("\nWhen you have typed in country and city, press 3 in the menu to see the weather forecast for your choice.\n")
menu = input("\nPress 1 for contry\nPress 2 for city\nPress 3 to see forecast\nPress 4 to exit\n")
if menu == "1":
rc.countryChoice()
elif menu == "2":
rc.cityChoice()
elif menu == "3":
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + countrychoice + "/" + citychoice + ".json")
data = r.json()
try:
for day in data['forecast']['simpleforecast']['forecastday']:
print (day['date']['weekday'] + ":")
print ("Conditions: ", day['conditions'])
print ("High: ", day['high']['celsius'] + "C", '\n' "Low: ", day['low']['celsius'] + "C", '\n')
except Exception as e:
print ("\nHave you typed in the correct country and city?\nBecause we got a" ,e, "error")
else:
print ("\nGoodbye")
break
我的主要程序如下所示:
class requestChoice:
def __init__(self):
self.countrychoice = None
self.citychoice = None
def countryChoice(self):
self.countrychoice = input("Enter which country your city is in(in english): ")
def cityChoice(self):
self.citychoice = input("Enter the name of the city: ")
from requestchoice import requestChoice
import requests
if __name__ == '__main__':
"""Introducion"""
print ("\nThis program lets you see a weather forecast for your choosen city.")
rc = requestChoice()
while True:
print("\nWhen you have typed in country and city, press 3 in the menu to see the weather forecast for your choice.\n")
menu = input("\nPress 1 for contry\nPress 2 for city\nPress 3 to see forecast\nPress 4 to exit\n")
if menu == "1":
rc.countryChoice()
elif menu == "2":
rc.cityChoice()
elif menu == "3":
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + countrychoice + "/" + citychoice + ".json")
data = r.json()
try:
for day in data['forecast']['simpleforecast']['forecastday']:
print (day['date']['weekday'] + ":")
print ("Conditions: ", day['conditions'])
print ("High: ", day['high']['celsius'] + "C", '\n' "Low: ", day['low']['celsius'] + "C", '\n')
except Exception as e:
print ("\nHave you typed in the correct country and city?\nBecause we got a" ,e, "error")
else:
print ("\nGoodbye")
break
当我运行我的程序时,我得到一个错误
NameError:name'countrychoice'未定义
。citychoice
也会出现同样的错误。我尝试在我的课堂上创建一个列表,并将countrychoice
附加到列表中,但没有成功。我应该如何使它按预期工作?您必须使用相应的对象名称访问它们。在这种情况下
rc.countrychoice
rc.citychoice
那么这条线
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + countrychoice + "/" + citychoice + ".json")
变成
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + rc.countrychoice + "/" + rc.citychoice + ".json")
您必须使用相应的对象名称访问它们。在这种情况下
rc.countrychoice
rc.citychoice
那么这条线
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + countrychoice + "/" + citychoice + ".json")
变成
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + rc.countrychoice + "/" + rc.citychoice + ".json")
您必须使用相应的对象名称访问它们。在这种情况下
rc.countrychoice
rc.citychoice
那么这条线
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + countrychoice + "/" + citychoice + ".json")
变成
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + rc.countrychoice + "/" + rc.citychoice + ".json")
您必须使用相应的对象名称访问它们。在这种情况下
rc.countrychoice
rc.citychoice
那么这条线
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + countrychoice + "/" + citychoice + ".json")
变成
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + rc.countrychoice + "/" + rc.citychoice + ".json")
您需要在此处使用
rc.countrychoice
和rc.citychoice
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + rc.countrychoice + "/" + rc.citychoice + ".json")
您需要在此处使用
rc.countrychoice
和rc.citychoice
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + rc.countrychoice + "/" + rc.citychoice + ".json")
您需要在此处使用
rc.countrychoice
和rc.citychoice
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + rc.countrychoice + "/" + rc.citychoice + ".json")
您需要在此处使用
rc.countrychoice
和rc.citychoice
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + rc.countrychoice + "/" + rc.citychoice + ".json")
您在此处收到
namererror
:
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + countrychoice + "/" + citychoice + ".json")
因为您没有定义名称
countrychoice
和citychoice
。也许你想用rc.countrychoice
和rc.citychoice
来代替?你在这里得到一个名称错误
:
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + countrychoice + "/" + citychoice + ".json")
因为您没有定义名称
countrychoice
和citychoice
。也许你想用rc.countrychoice
和rc.citychoice
来代替?你在这里得到一个名称错误
:
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + countrychoice + "/" + citychoice + ".json")
因为您没有定义名称
countrychoice
和citychoice
。也许你想用rc.countrychoice
和rc.citychoice
来代替?你在这里得到一个名称错误
:
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + countrychoice + "/" + citychoice + ".json")
因为您没有定义名称countrychoice
和citychoice
。也许你想用rc.countrychoice
和rc.citychoice
来代替?请你通过准确的回溯,好吗?请你通过准确的回溯,好吗?请你通过准确的回溯,好吗?