0

I am trying to build a cost model using mostly if statements but I keep getting the error:

NameError: name 'spread_1' is not defined. 

I am a beginner with python so I don't know if I've done something incorrect. Please help.

#water depth of project site in metres
water_depth = 100

#platform wells require jackups
pl = 1
#1-2 complexity required LWIV
ss_simple = 1
#3-4 complexity requires rig
ss_complex = 0

#day rates
vjackup = 75000
jackup = 90000
vsemi = 170000
semi = 300000
lwiv = 200000

#determining vessel spread for platform wells
if pl >= 1:
    if water_depth == range(0, 50):
        spread_1 = vjackup * 24.1
    elif water_depth == range(51, 150):
        spread_1 = jackup * 24.1
elif pl == 0:
    spread_1 = 0
Red
  • 26,798
  • 7
  • 36
  • 58
fb03
  • 11
  • 2
  • 1
    What if pl is not 0 neither is equal or greater than 1. For example pl == 0.5? – Fernando Aug 09 '21 at 12:32
  • pl is number of wells so can only be 0 or a number equal or greater than 1 – fb03 Aug 09 '21 at 13:02
  • `spread_1` is never set because `water_depth == range(...)` is checking whether `water_depth` is a `range` object with the same bounds, not whether it is within the bounds. See the linked duplicate for how to check correctly. – snakecharmerb Aug 10 '21 at 15:25

1 Answers1

1

You should replace the == with in at the

if water_depth == range(0, 50):

and

elif water_depth == range(51, 150):

making your block of code:

if pl >= 1:
    if water_depth == range(0, 50):
        spread_1 = vjackup * 24.1
    elif water_depth == range(51, 150):
        spread_1 = jackup * 24.1
elif pl == 0:
    spread_1 = 0

into

if pl >= 1:
    if water_depth in range(0, 50):
        spread_1 = vjackup * 24.1
    elif water_depth in range(51, 150):
        spread_1 = jackup * 24.1
elif pl == 0:
    spread_1 = 0

But it would be more practical to use extreme equality operators in your case:

if pl >= 1:
    if 0 <= water_depth < 50:
        spread_1 = vjackup * 24.1
    elif 51 <= water_depth < 150:
        spread_1 = jackup * 24.1
elif pl == 0:
    spread_1 = 0

Do note that the range() function omits the end value, so range(0, 50)'s last value would be 49, not 50.

Red
  • 26,798
  • 7
  • 36
  • 58