简体   繁体   English

我有这个问题

[英]I'm having trouble with this

def get_age():
    age = int(input())
    if age < 18 or age > 75:
        raise ValueError('Invalid age.')
    return age


def fat_burning_heart_rate(age):

    heart_rate = (220 * 0.70) - age
    return heart_rate

if __name__ == "__main__":
    try:
        heart_rate = fat_burning_heart_rate(age)
        age = get_age()
        print('Fat burning heart rate for a', age, 'year-old:', 
               heart_rate, 'bpm')

    except ValueError as e:
        print(e)
        print('Could not calculate heart rate info.')

The primary issue that I have so far is when I go run this code it generates a name error saying that 'age' is not defined.到目前为止,我遇到的主要问题是当我 go 运行此代码时,它会生成一个名称错误,指出未定义“年龄”。 This occurs when I try to assign heart_rate = fat_burning_heart_rate(age).当我尝试分配 heart_rate = fat_burning_heart_rate(age) 时会发生这种情况。 I suspect that it has something to do with the way that I pass the function parameter.我怀疑这与我传递 function 参数的方式有关。 But what can I do to fix it?但是我能做些什么来解决它?

def fat_burning_heart_rate(age):

heart_rate = (220 * 0.70) - age
if age < 18 or age > 75:
    raise ValueError('Invalid age.')
return heart_rate, age

if __name__ == "__main__":
  try:
    age = int(input("Input age: "))
    heart_rate, age = fat_burning_heart_rate(age)
    print('Fat burning heart rate for a', age, 'year-old:',
          heart_rate, 'bpm')

except ValueError as e:
    print(e)
    print('Could not calculate heart rate info.') 

You don't need get_age().你不需要 get_age()。 You can add that code to fat_burning_heart_rate().您可以将该代码添加到 fat_burning_heart_rate()。

age = get_age() seemed to be in the wrong position within the code, also I believe you misinterpreted the expression for heart rate. age = get_age() 在代码中似乎是错误的 position,而且我相信你误解了心率的表达。 This code will allow for both definitions and will properly flag any errors.此代码将允许两种定义,并将正确标记任何错误。

def get_age():
    age = int(input())
    if age < 18 or age > 75:
        raise ValueError('Invalid age.')
    return age

def fat_burning_heart_rate(age):
    heart_rate = (220 - age) * .7
    return heart_rate


if __name__ == "__main__":
    try:
        age = get_age()
        heart_rate = fat_burning_heart_rate(age)
        print('Fat burning heart rate for a', age, 'year-old:', 
heart_rate, 'bpm')

    except ValueError as e:
        print(e)
        print('Could not calculate heart rate info.')

I solved it with this code.我用这段代码解决了它。

def get_age():          #gets user input and checks to see if valid age range
    age = int(input())
    if age < 18 or age > 75:
        raise ValueError('Invalid age.')
    return age


def fat_burning_heart_rate(age):   #calc heart rate with given user input(age)
    heart_rate = (220 - age) * .70
    return heart_rate

if __name__ == "__main__":

    try:
        age = get_age()         #calls def get_age and fat_burning_heart_rate
        bpm = fat_burning_heart_rate(age)
        print('Fat burning heart rate for a', age, 'year-old:', bpm, 'bpm')
    except ValueError as excpt:
        print(excpt)
        print('Could not calculate heart rate info.')

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

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