名称(变量名)未定义错误

这应该有效:


import pandas as pd


df_ex = pandas.read_csv('ex.csv', sep=',')

latitude_diff_list, longitude_diff_list = [], []

for idx,row in df_ex[1:].iterrows():

    if abs(row['latitude'] - df_ex.loc[idx-1, 'latitude']) > 0.1:

        latitude_diff_list.extend([idx-1, idx])

    if abs(row['longitude'] - df_ex.loc[idx-1, 'longitude']) > 0.1:

        longitude_diff_list.extend([idx-1, idx])


latitude_diff_list, longitude_diff_list = list(set(latitude_diff_list)), list(set(longitude_diff_list))

分享


慕标5832272
浏览 97回答 5
5回答

慕神8447489

您可以将其放入一个类中,其中每个对象都存储每个人的体重数据。class bmi_profile:&nbsp; &nbsp; def __init__(self, name, weight_kg, height_m):&nbsp; &nbsp; &nbsp; &nbsp; self.name = name&nbsp; &nbsp; &nbsp; &nbsp; self.weight_kg = weight_kg&nbsp; &nbsp; &nbsp; &nbsp; self.height_m = height_m&nbsp; &nbsp; &nbsp; &nbsp; self.bmi_result = weight_kg / (height_m ** 2)&nbsp;&nbsp;&nbsp; &nbsp; def print_bmi(self):&nbsp; &nbsp; &nbsp; &nbsp; print(self.name, end = ' ')&nbsp; &nbsp; &nbsp; &nbsp; if self.bmi_result < 25:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Is not overweight!')&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Overweight!')name = input('Name: ')weight = float(input('Weight(Kg): '))height = float(input('Height(M): '))profile = bmi_profile(name, weight, height)profile.print_bmi()输出:(在 后输入:)Name: JonWeight(Kg): 60Height(M): 1.80Jon Is not overweight!

慕桂英4014372

# My first trydef profile():&nbsp; global weight_kg, height_m, name&nbsp; name = input('Name: ')&nbsp; weight_kg = int(input('Weight(Kg): '))&nbsp; height_m = int(input('Height(M): '))def bmi_result():&nbsp; &nbsp; print(name)&nbsp; &nbsp; if bmi_converter < 25:&nbsp; &nbsp; &nbsp; &nbsp; print('Is not overweight!')&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print('Overweight!')profile()bmi_converter = (weight_kg / (height_m ** 2))&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;bmi_result()不错的尝试,乔恩。您需要使用全局变量才能工作。另外,您忘记引用正确的变量bmi_converter

翻翻过去那场雪

你可以试试这个:def profile():&nbsp; &nbsp; name = input('Name: ')&nbsp; &nbsp; weight_kg = int(input('Weight(Kg): '))&nbsp; &nbsp; height_m = int(input('Height(M): '))&nbsp; &nbsp; bmi_converter(weight_kg, height_m, name)def bmi_converter(weight_kg, height_m, name):&nbsp; &nbsp; bmi = weight_kg / (height_m ** 2)&nbsp; &nbsp; bmi_result(bmi, name)def bmi_result(bmi, name):&nbsp; &nbsp; print(name)&nbsp; &nbsp; if bmi < 25:&nbsp; &nbsp; &nbsp; &nbsp; print('Is not overweight!')&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print('Overweight!')profile()

凤凰求蛊

您的weight_kg、height_m 和name 变量范围位于profile 函数中。所以它们不能用于其他功能。您可以使用这些变量作为参数。&nbsp; &nbsp; def profile():&nbsp; &nbsp; &nbsp; &nbsp; name = input('Name: ')&nbsp; &nbsp; &nbsp; &nbsp; weight_kg = int(input('Weight(Kg): '))&nbsp; &nbsp; &nbsp; &nbsp; height_m = int(input('Height(M): '))&nbsp; &nbsp; &nbsp; &nbsp; bmi_converter(name,weight_kg, height_m)&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; def bmi_converter(name,weight_kg, height_m):&nbsp; &nbsp; &nbsp; &nbsp; bmi_formula&nbsp; = weight_kg / (height_m ** 2)&nbsp; &nbsp; &nbsp; &nbsp; bmi_result(bmi_formula , name)&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; def bmi_result(bmi, name):&nbsp; &nbsp; &nbsp; &nbsp; print(name)&nbsp; &nbsp; &nbsp; &nbsp; if bmi < 25:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Is not overweight!')&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Overweight!')&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; profile()

SMILET

我终于让我的代码工作了,我将你们的一些代码打乱到我的代码中,并使其更短、更简单,如下:name = input('Name: ')weight_kg = int(input('Weight(Kg): '))height_m = int(input('Height(M): '))def bmi_calculator(name, weight_kg, height_m):&nbsp; &nbsp; bmi = weight_kg / (height_m ** 2)&nbsp; &nbsp; if bmi < 25:&nbsp; &nbsp; &nbsp; &nbsp; print(name +' is not overweight!')&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print(name +' is overweight!')result = bmi_calculator(name, weight_kg, height_m)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python