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
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.
Related
I've learnt how to simulate s many games as I want, but the problem is, I want the dataset(mean median mode) updated for each team after every game... total score of each team for every game and combined total. I had a lot of errors with the traditional sum / len concept because it wants a dataset manually imported.
import random
import time
import math
michigan = 0
kansas = 0
seconds = 2400
michiganw = 0
kansasw = 0
while True:
michigan = 0
kansas = 0
seconds = 2400
if kansasw == 10:
print("Kansas Wins", kansasw)
print("Michigan Wins", michiganw)
("Kansas 100 Wins")
print("Kansas Game Score", kansasgamescore)
break
elif michiganw == 10:
print("Michigan Wins", michiganw)
print("Kansas Wins", kansasw)
print("Michigan 100 Wins")
print("Kansas Game Score", kansasgamescore)
break
for i in range(150):
Goblinsi = random.randint(0, 24) #kansas or michigan shot attempt
if Goblinsi <= 8: #kansas scores
kansas = kansas + 2
Goblinsii = random.randint(0, 4)
if Goblinsii == 4: #1pt extra for 3 point fg
kansas = kansas + 1
seconds = seconds - 20
elif Goblinsii < 4:
seconds = seconds - 20
else:
seconds = seconds - 20
if Goblinsi >= 16: #michigan scores
michigan = michigan + 2
Goblinsii = random.randint(0, 4)
if Goblinsii == 4: #1pt extra for 3 point fg
michigan = michigan + 1
seconds = seconds - 20
elif Goblinsii < 4:
seconds = seconds - 20
if seconds == 0: #free throws
kansas = kansas + 10
michigan = michigan + 10
if michigan > kansas:
print("Michigan Win!")
print("Final Score")
print("Michigan", michigan)
print("Kansas", kansas)
#I want this to be the point the mean, median, mode are calculated and listed*
print("Next Game")
michiganw = michiganw + 1
michigan = 0
kansas = 0
seconds = 2400
continue
elif michigan < kansas:
print("Kansas Win!")
print("Final Score")
print("Michigan", michigan)
print("Kansas", kansas)
#I want this to be the point the mean, median, mode are calculated and listed*
print("Play Again")
kansasw = kansasw + 1
michigan = 0
kansas = 0
seconds = 2400
continue
elif michigan == kansas:
Goblinsi = random.randint(0, 12)
if Goblinsi >= 7:
kansas = kansas + 1
print("Kansas Win")
print("Final Score")
print("Michigan", michigan)
print("Kansas", kansas)
michigan = 0
kansas = 0
seconds = 2400
kansasw = kansasw + 1
continue
elif Goblinsi <= 6:
michigan = michigan + 1
print("Michigan Win")
print("Final Score")
print("Michigan", michigan)
print("Kansas", kansas)
michigan = 0
kansas = 0
seconds = 2400
michiganw = michiganw + 1
continue
The following code is supposed to calcualte what the savings rate would be to pay off my down payment in exactly 36 months given certain factors like (salary raises and annual return rates. I am having trouble getting the output of the range function to interact with my bisection search method.
# Cost
total_cost = 1000000
portion_down_payment = .25 * total_cost
# Salary
annual_salary = 150000
semi_annual_raise = .07
annual_return_rate = .04
current_savings = 0
month = 0
# Bisection
epsilon = 100
num_guesses = 0
low = 0
high = 1
saving_rate = (high + low) / 2.0
portion_saved_monthly = annual_salary / 12 * saving_rate
# range()function
for x in range(36):
current_savings += (current_savings * annual_return_rate / 12) + portion_saved_monthly
month += 1
if month % 6 == 0:
annual_salary += annual_salary * semi_annual_raise
portion_saved_monthly = annual_salary / 12 * saving_rate
while abs(current_savings - portion_down_payment) >= epsilon:
if current_savings < portion_down_payment:
low = saving_rate
else:
high = saving_rate
saving_rate = (high + low) / 2.0
num_guesses += 1
print('Best savings rate: ', saving_rate)
print('Steps in bisection search: ', num_guesses)
I started with the while loop and nested the for loop inside. I also moved some variables inside the while loop which helped me get the right answer.
'''
# Cost of House
total_cost = 1000000
portion_down_payment = .25 * total_cost
# Salary
annual_salary_input = float(input('Enter the starting salary: '))
current_savings = 0
# Bisection
epsilon = 100
num_guesses = 0
low = 0
high = 1
saving_rate = (high + low) / 2.0
portion_saved_monthly = annual_salary_input / 12 * saving_rate
while abs(current_savings - portion_down_payment) >= epsilon:
annual_salary = annual_salary_input
semi_annual_raise = .07
annual_return_rate = .04
current_savings = 0
month = 0
portion_saved_monthly = annual_salary / 12 * saving_rate
saving_rate = (high + low) / 2.0
# Loop
for x in range(36):
current_savings += (current_savings * annual_return_rate / 12) + portion_saved_monthly
month += 1
if month % 6 == 0:
annual_salary += annual_salary * semi_annual_raise
portion_saved_monthly = annual_salary / 12 * saving_rate
if current_savings < portion_down_payment:
low = saving_rate
else:
high = saving_rate
saving_rate = (high + low) / 2.0
num_guesses += 1
if num_guesses > 1000:
break
if saving_rate < 1.0:
print('Best savings rate: ', "%.4f" % saving_rate)
print('Steps in bisection search: ', num_guesses)
else:
print('It is not possible to pay the down payment in three years.')
'''
Some of the vars are in Portuguese sorry. I am trying to show how much of which type of money bills you will and will not get on a atm:
example:
IN: $123,45
OUT:1 OF $100 BILL, 0 OF $50 BILL, 1 OF $20 BILL, 0 OF $10 BILL and etc.
This is what I did so far but I can't do the bill that will not come on the atm.
r = 0
print('='*20)
print('{:^20}'.format('CAIXA ELETRÔNICO'))
print('{:^20}'.format(' Banco do Romeu '))
print('='*20)
caixa = float(input('Qual serĂ¡ o valor sacado? '))
total = caixa
ced = 100
totalced = 0
while True:
if total >= ced:
total = total - ced
totalced += 1
else:
if totalced > 0:
print(f'{totalced} notas(s) de R${ced}')
elif ced == 100:
ced = 50
elif ced == 50:
ced = 20
elif ced == 20:
ced = 10
elif ced == 10:
ced = 5
elif ced == 5:
ced = 2
elif ced == 2:
ced = 1
elif ced == 1:
ced = 0.50
elif ced == 0.50:
ced = 0.25
elif ced == 0.25:
ced = 0.10
elif ced == 0.10:
ced = 0.05
elif ced == 0.05:
ced = 0.01
totalced = 0
if total == 0:
break
You can do a greedy approach and try to subtract the highest value bill and keep a count of how many times you subtracted from each bill. For example:
423,45
You start with $100, you can subtract it 4 times. You're left with 23,45.
Then you go on to $50. You can't subtract $50 from 23,45 so you go to the next highest bill, $20.
You can subtract one $20 from 23,45 and you're left with ,45.
You keep on going and keep count until you can't subtract anymore. Then you print out the count of subtractions from each bill. Hope that makes sense!
You can subtract value from initial value.
variable valor is the initial value
valor = 162
cells = []
money = [100, 50, 20, 10, 5, 2]
for _ in money:
while True:
if valor >= _:
if valor - _ < money[-1] or (valor % _) % money[-1] != 0:
if valor == _:
cells.append(_)
break
valor -= _
cells.append(_)
else:
break
print(cells)
>>> [100, 50, 10, 2]
I'm currently running Python 3.5 on Mac and I can't seem to find a way to be more efficient with my code.
In all I'm currently making a wheel of fortune game (or basically hangman), and I have it so that when the user clicks on a letter it checks if the letter is in the word etc.
Is there a better way than writing 26 if statements and checking coordinates each time? Is there a more efficient way of checking for coordinates using if statements?
while True:
mouse = win.getMouse()
if 35 < mouse.x < 65 and 85 < mouse.y < 115:
ans = "a"
break
elif 65 < mouse.x < 95 and 85 < mouse.y < 115:
ans = "b"
break
elif 95 < mouse.x < 125 and 85 < mouse.y < 115:
ans = "c"
break
elif 125 < mouse.x < 155 and 85 < mouse.y < 115:
ans = "d"
break
elif 155 < mouse.x < 185 and 85 < mouse.y < 115:
ans = "e"
break
elif 185 < mouse.x < 215 and 85 < mouse.y < 115:
ans = "f"
break
elif 215 < mouse.x < 245 and 85 < mouse.y < 115:
ans = "g"
break
elif 245 < mouse.x < 275 and 85 < mouse.y < 115:
ans = "h"
break
elif 275 < mouse.x < 305 and 85 < mouse.y < 115:
ans = "i"
break
elif 305 < mouse.x < 335 and 85 < mouse.y < 115:
ans = "j"
break
elif 35 < mouse.x < 65 and 115 < mouse.y < 145:
ans = "k"
break
elif 65 < mouse.x < 95 and 115< mouse.y < 145:
ans = "l"
break
elif 95 < mouse.x < 125 and 115 < mouse.y < 145:
ans = "m"
break
elif 125 < mouse.x < 155 and 115 < mouse.y < 145:
ans = "n"
break
elif 155 < mouse.x < 185 and 115 < mouse.y < 145:
ans = "o"
break
elif 185 < mouse.x < 215 and 115 < mouse.y < 145:
ans = "p"
break
elif 215 < mouse.x < 245 and 115 < mouse.y < 145:
ans = "q"
break
elif 245 < mouse.x < 275 and 115 < mouse.y < 145:
ans = "r"
break
elif 275 < mouse.x < 305 and 115 < mouse.y < 145:
ans = "s"
break
elif 305 < mouse.x < 335 and 115 < mouse.y < 145:
ans = "t"
break
elif 35 < mouse.x < 65 and 145 < mouse.y < 175:
ans = "u"
break
elif 65 < mouse.x < 95 and 145 < mouse.y < 175:
ans = "v"
break
elif 95 < mouse.x < 125 and 145 < mouse.y < 175:
ans = "w"
break
elif 125 < mouse.x < 155 and 145 < mouse.y < 175:
ans = "x"
break
elif 155 < mouse.x < 185 and 145 < mouse.y < 175:
ans = "y"
break
elif 185 < mouse.x < 215 and 145 < mouse.y < 175:
ans = "z"
break
As simple solution using a calculation vs. multiple if statements:
import string
while True:
mouse = win.getMouse()
x, xr = divmod(mouse.x-35, 30)
y, yr = divmod(mouse.y-85, 30)
if not(0 <= x < 10 and 0 <= y < 3): # Guard values
continue
if xr == 0 or yr == 0: # Gridlines?
continue
try:
ans = string.ascii_lowercase[10*y+x]
break
except IndexError:
pass
e.g. mouse(x=102, y=143) would return 'm'
You could use the following code to avoid the big if statement.
def check_pos(x, y):
table = 'abcdefghijklmnopqrstuvwxyz'
idx = (x - (35-30)) / 30
idy = (y - (85-30)) / 30
# each row has 10 cells
offset = idx + (idy -1) * 10
if 1 <= offset <= 26:
return table[offset-1]
import string
alpha=string.ascii_lowercase
mouse = win.getMouse()
for idx,i in enumerate(range(35,335,30)):
if mouse.x>i and mouse.x<i+30:
if mouse.y>85 and mouse.y<115:
ans=alpha[idx]
break
elif mouse.y>115 and mouse.y<145:
ans=alpha[idx + 10]
break
elif mouse.y>145 and mouse.y<175:
ans=alpha[idx+20]
break
I need help to compile my payroll using the following python formula for OpenERP. Can someone please help me to write this properly for payroll please.
if categories.BASIC < 120:
result = 0
elif categories.BASIC < 180:
result = - categories.BASIC - 120 * 0.05
elif categories.BASIC < 264:
result = - categories.BASIC - 180 * 0.1 + 3
elif categories.BASIC < 2136:
result = - categories.BASIC - 264 * 0.175 + 11.4
else: categories.BASIC > 2400:
result = - categories.BASIC - 2400 * 0.25 + 214.55
Kind Regards
Don't for get to indent, python won't work without proper indentation.
if categories.BASIC < 120:
result = 0
elif categories.BASIC < 180:
result = categories.BASIC - 120 * 0.05
elif categories.BASIC < 264:
result = categories.BASIC - 180 * 0.1 + 3
elif categories.BASIC < 2136:
result = categories.BASIC - 264 * 0.175 + 11.4
elif categories.BASIC > 2400: #the else statement can't have a condition
result = categories.BASIC - 2400 * 0.25 + 214.55