简体   繁体   English

类型错误:x 缺少 1 个必需的位置参数:y

[英]TypeError: x missing 1 required positional argument: y

I am trying to import and call a function from my refactor file into my init file.我正在尝试将 function 从我的重构文件导入并调用到我的初始化文件中。 However, when I attempt to call the function in one of the routes, I get this error in the terminal, " TypeError: show_state_locations() missing 1 required positional argument: 'test_code' "但是,当我尝试在其中一条路由中调用 function 时,我在终端中收到此错误,“ TypeError: show_state_locations() missing 1 required positional argument: 'test_code'

Here is my code and how I am importing everything:这是我的代码以及我如何导入所有内容:

refactor重构

import requests
from privateinfo import key

def test_code(state, API_BASE_URL):
    url = f'https://covid-19-testing.github.io/locations/{state.lower()}/complete.json'
    res = requests.get(url)
    testing_data = res.json()
    latsLngs = {}
    for obj in testing_data:
          if obj["physical_address"]:

            for o in obj["physical_address"]:
                    addy = o["address_1"] 
                    city = o["city"]
                    phone = obj["phones"][0]["number"]

            location = f'{addy} {city}'
            res2 = requests.get(API_BASE_URL,
                                params={'key': key, 'location': location})

            location_coordinates = res2.json()
            lat = location_coordinates["results"][0]["locations"][0]["latLng"]["lat"]
            lng = location_coordinates["results"][0]["locations"][0]["latLng"]["lng"]
            latsLngs[location] = {'lat': lat, 'lng': lng, 'place': location, 'phone': phone}

init在里面

from .refactor import test_code


@app.route('/location')
def show_state_locations(test_code):
    """Return selected state from drop down menu"""
    state = request.args.get('state')
    test_code(state, API_BASE_URL)

    return render_template('location.html', latsLngs=latsLngs)

You are assuming that a name is persisted from one function call to its outer scope:您假设一个名称是从一个 function 调用到其外部 scope 的持久化:

def f():
    x = 1

f()
print(x)
NameError: name x is not defined

You need to return the value and assign the name to x in the calling scope for this to work您需要在调用 scope 中返回值并将名称分配给x才能正常工作

def f():
    return 1

x = f()
x
1

Note that return x doesn't work either, because it's the value that's being returned, not the name:请注意, return x也不起作用,因为它是返回的,而不是名称:

def f():
    x = 1
    return x

f()
x
# NameError!

x = f()
x
1

The same is happening to latLng : latLng也是如此:

def test_code():
    latLng = {}

test_code()
latLng = latLng
#NameError!

Change it to将其更改为

def test_code():
    latLng = {}
    ...
    return latLng

latLng = test_code()
latLng = latLng
# no error

暂无
暂无

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

相关问题 TypeError:scatter()缺少1个必需的位置参数:“ y” - TypeError: scatter() missing 1 required positional argument: 'y' 类型错误:流()缺少 1 个必需的位置参数:'x' - TypeError: flow() missing 1 required positional argument: 'x' 类型错误:flag1() 缺少 1 个必需的位置参数:'y' - TypeError: flag1() missing 1 required positional argument: 'y' 类型错误:predict() 缺少 1 个必需的位置参数:'y_train' - TypeError: predict() missing 1 required positional argument: 'y_train' TypeError: fit() 缺少 1 个必需的位置参数:'y' while GridSearching CNN - TypeError: fit() missing 1 required positional argument: 'y' while GridSearching CNN 实现逻辑回归“TypeError: fit() missing 1 required positional argument: 'y'” - Implementing Logistic Regression “TypeError: fit() missing 1 required positional argument: 'y'” 逻辑回归类型错误:fit() 缺少 1 个必需的位置参数:'y' - Logistic Regression TypeError: fit() missing 1 required positional argument: 'y' 类型错误:fit() 缺少 1 个必需的位置参数:'y'(使用 sklearn - ExtraTreesRegressor) - TypeError: fit() missing 1 required positional argument: 'y' (using sklearn - ExtraTreesRegressor) scikit-learn-TypeError:fit()缺少1个必需的位置参数:“ y” - scikit-learn - TypeError: fit() missing 1 required positional argument: 'y' Python 出错:TypeError:fit() 缺少 1 个必需的位置参数:'y' - Error with Python: TypeError: fit() missing 1 required positional argument: 'y'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM