简体   繁体   English

NameError:未定义名称“ countrychoice”

[英]NameError: name 'countrychoice' is not defined

I'm having some trouble with the current program I'm writing. 我正在编写的当前程序遇到问题。

I'm letting the user type in a country, and then a city city in that country, and then see a weather forecast for the choosen city using API. 我让用户输入一个国家,然后输入该国家的城市,然后使用API​​查看所选城市的天气预报。

I'm using a class, like this: 我正在使用一个类,像这样:

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: ")

And my main program looks like this: 我的主程序如下所示:

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

When I run my program I get the error NameError: name 'countrychoice' is not defined . 当我运行程序时,出现错误NameError: name 'countrychoice' is not defined It is going to be the same error with the citychoice . citychoice将会出现相同的错误。 I've tried creating a list in my class and append the countrychoice to the list but without any luck. 我尝试在班级中创建一个列表,并将countrychoice附加到列表中,但是没有任何运气。 How am I supposed to make it work as wished? 我应该如何使其按需工作?

You have to access them with the corresponding object name. 您必须使用相应的对象名称访问它们。 In this case 在这种情况下

rc.countrychoice
rc.citychoice

So, this line 所以这条线

r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + countrychoice + "/" + citychoice + ".json")

becomes 变成

r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + rc.countrychoice + "/" + rc.citychoice + ".json")

您需要在此处使用rc.countrychoicerc.citychoice

    r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + rc.countrychoice + "/" + rc.citychoice + ".json")

You are getting a NameError on here: 您在此处收到NameError

r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + countrychoice + "/" + citychoice + ".json")

because you have no names countrychoice and citychoice defined. 因为您没有定义countrychoicecitychoice名称。 Perhaps you meant to use rc.countrychoice and rc.citychoice instead? 也许您打算使用rc.countrychoicerc.citychoice代替?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM