Python function NameError [duplicate]

爱⌒轻易说出口 提交于 2019-11-26 14:56:19

问题


I am using python 3.4.1. I keep getting this error,

 (NameError: name 'calcHullSpeed' is not defined)

What am I doing wrong?

import math

LWL = float(input("Enter the length of the boat: "))
maxHullSpeed = calcHullSpeed(LWL)
print("The hull speed is: " + str(round(maxHullSpeed,2)))

def calcHullSpeed(LWL):
    maxHS = 1.34*math.sqrt(LWL)
    return maxHS

回答1:


Put your function first:

def calcHullSpeed(LWL):
    maxHS = 1.34*math.sqrt(LWL)
    return maxHS

LWL = float(input("Enter the length of the boat: "))
maxHullSpeed = calcHullSpeed(LWL)
print("The hull speed is: " + str(round(maxHullSpeed,2)))

Functions have to be defined before you call them.



来源:https://stackoverflow.com/questions/26152661/python-function-nameerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!