为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕标5832272 2023-09-12 17:50:29
这应该有效:import pandas as pddf_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))分享
查看完整描述

5 回答

?
慕神8447489

TA贡献1780条经验 获得超1个赞

您可以将其放入一个类中,其中每个对象都存储每个人的体重数据。


class bmi_profile:

    def __init__(self, name, weight_kg, height_m):

        self.name = name

        self.weight_kg = weight_kg

        self.height_m = height_m

        self.bmi_result = weight_kg / (height_m ** 2)

  

    def print_bmi(self):

        print(self.name, end = ' ')

        if self.bmi_result < 25:

            print('Is not overweight!')

        else:

            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: Jon

Weight(Kg): 60

Height(M): 1.80

Jon Is not overweight!


查看完整回答
反对 回复 2023-09-12
?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

# My first try

def profile():

  global weight_kg, height_m, name

  name = input('Name: ')

  weight_kg = int(input('Weight(Kg): '))

  height_m = int(input('Height(M): '))




def bmi_result():

    print(name)

    if bmi_converter < 25:

        print('Is not overweight!')

    else:

        print('Overweight!')


profile()


bmi_converter = (weight_kg / (height_m ** 2))        


bmi_result()

不错的尝试,乔恩。您需要使用全局变量才能工作。另外,您忘记引用正确的变量bmi_converter

https://img1.sycdn.imooc.com//6500349b00019b8201910106.jpg

查看完整回答
反对 回复 2023-09-12
?
翻翻过去那场雪

TA贡献2065条经验 获得超13个赞

你可以试试这个:


def profile():

    name = input('Name: ')

    weight_kg = int(input('Weight(Kg): '))

    height_m = int(input('Height(M): '))

    bmi_converter(weight_kg, height_m, name)



def bmi_converter(weight_kg, height_m, name):

    bmi = weight_kg / (height_m ** 2)

    bmi_result(bmi, name)



def bmi_result(bmi, name):

    print(name)

    if bmi < 25:

        print('Is not overweight!')

    else:

        print('Overweight!')



profile()


查看完整回答
反对 回复 2023-09-12
?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

您的weight_kg、height_m 和name 变量范围位于profile 函数中。所以它们不能用于其他功能。您可以使用这些变量作为参数。


    def profile():

        name = input('Name: ')

        weight_kg = int(input('Weight(Kg): '))

        height_m = int(input('Height(M): '))

        bmi_converter(name,weight_kg, height_m)

    

    

    def bmi_converter(name,weight_kg, height_m):

        bmi_formula  = weight_kg / (height_m ** 2)

        bmi_result(bmi_formula , name)

    

    

    def bmi_result(bmi, name):

        print(name)

        if bmi < 25:

            print('Is not overweight!')

        else:

            print('Overweight!')

    

    

    profile()


查看完整回答
反对 回复 2023-09-12
?
SMILET

TA贡献1796条经验 获得超4个赞

我终于让我的代码工作了,我将你们的一些代码打乱到我的代码中,并使其更短、更简单,如下:


name = input('Name: ')

weight_kg = int(input('Weight(Kg): '))

height_m = int(input('Height(M): '))


def bmi_calculator(name, weight_kg, height_m):

    bmi = weight_kg / (height_m ** 2)

    if bmi < 25:

        print(name +' is not overweight!')

    else:

        print(name +' is overweight!')


result = bmi_calculator(name, weight_kg, height_m)


查看完整回答
反对 回复 2023-09-12
  • 5 回答
  • 0 关注
  • 124 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信