Setting condition in Salary Head in OpenERP - python

I'm implementing HR module in OpenERP I am trying to setup a condition in Salary Head like:
if Basic > 0 and basic <=5000:
ProfessionalTax = 80
else if
Basic > 5000 and basic <= 10000
ProfessionalTax = 150
Can anybody help me with the syntax? I have not had any luck finding this information.

The Python if statement uses elif to mean else if. Your example would be as follows:
if basic > 0 and basic <=5000:
ProfessionalTax = 80
elif basic > 5000 and basic <= 10000:
ProfessionalTax = 150
I also had to move the condition up onto the same line as elif. Remember that line breaks and white space have meaning in Python. Variable names are case-sensitive, so I guessed that you only had one variable named basic and made them all the same.

Related

Perhaps I fundamentally misunderstand indentation in python? - Python

This code gives me an indentation error on checks. I get that this happens often, but the instance is in between two for loops that exist because I need to reference two different lists.
I do not even have the data set made yet, but it should report that the syntax is correct at least. The code is fairly simple. I want to automate package placement in a building and I want to do so by taking the biggest packages and putting them in place with the least amount of room where it would still fit.
All inputs that I used so far are dictionaries because I need to know which shelf I am referring too. I am this close to turning it to lists and being extremely strict about formatting.
inv = maxkey["Inventory"]
is the line where the mistake happens. I do not know how to fix it. Should I use lists for this project instead? Is there a flaw in the logic? Is there a parentheses I forgot? Please let me know if this is just an oversight on my part. Please contact me for further details.
def loadOrder(inProd, units, loc, pref, shelves):
items = len(inProd)
while items > 0
# What is the biggest package in the list?
mxw = 0 # Frontal area trackers
BoxId = {} # Identifies what is being selected
for p in inProd:
if p["Height"]*p["Width"] > mxw:
mxw = p["Width"]*p["Height"]
BoxId = p
else:
pass
# What is the location with the least amount of space?
maxi = 0.001
maxkey = {}
for key in loc:
if key["Volume Efficiency"] > maxi and key["Width"] > mxw/BoxId["Height"]:
maxi = key["Volume Efficiency"]
maxkey = key
else:
pass
maxkey["Inventory"].append(BoxId)
weight = 0
volTot = 0
usedL = 0
inv = maxkey["Inventory"]
for k in inv:
weight = k['Weight']+weight
vol = k['Height']*k['Width']*k['Depth']+volTot
usedL = k['Width']+usedL
maxkey["Volume Efficiency"] = volTot/(maxkey['Height']*maxkey['Weight']*maxkey['Depth'])
maxkey['Width Remaining'] = usedL
maxkey['Capacity Remaining'] = weight
del inProd[BoxId]
items = len(inProd)
return [inProd, units, loc, pref, shelves]
Indentation in a function definition should be like:
def function-name():
<some code>
<return something>
Also, you have missed : after while loop condition.
It shoulde be while items > 0:
And you should not mixing the use of tabs and spaces for indentation.
The standard way for indentation is 4 spaces.
you can see more in PEP 8.

Using nested IF statements for text based RPG skill point allocation with python

I am creating a text based RPG where the player starts with 600 skill points to allocate between 6 skills.
So I start by assigning the value 600 to the skill_points variable.
skill_points = 600
then I assign a default value for each skills variable.
skill_1 = 0
skill_2 = 0
skill_3 = 0
Now I ask the player for input for the first skill.
skill1_input = int(input("Skill 1:"))
And I update the value of the variables.
skill_1 = skill_1 + skill1_input
skill_points = skill_points - skill1_input
Then I use a if statement to check if the number submitted is above the leftover skill points available, If not It prompts you to input the next skill.
if skill1_input > skill_points:
print("Not Valid")
else:
skill_2input = int(input("Skill 2:"))
Nested IF/ELSE statements repeat throughout all 6 skills until you allocate all your points. It is in a while loop so that if you don't use all of your skill points, it starts over at the first skill.
However, it is very finicky. At first I put 100 points into each skill and it worked fine. When I put 100 points into the first skill then 200 into the next skill, it prints not valid, even though there 500 points left and 200 is not is not more then 500.
There are multiple similar scenarios where the math should work properly yet the program still prints not valid
What is the current way to do this? Should I have not designed it using if statements?
This can be better done with a for loop instead of nested if statements, and I would use an array for the skills.
skill_points = 600
skills = []
skill_inputs = 0
for i in range(6):
skill_input = int(input("Skill %i: "%(i+1)))
if skill_inputs + skill_input > skill_points:
print("Not Valid")
break
else:
skills.append(skill_input)
skill_inputs += skill_input
I rewrote your code and its functional to what you asked, but as an advice, before writing code, make sure your program makes fully sense to what you want to build(even if you don't know the tools to build it yet. That is what makes you learn more!)
If your code repeats itself, theres probably a better way to write it. As you can see, that code basically repeats itself 6 times.
You could replace it with:
skill_points = 600
skill_base = []
skill = 0
for i in range(6):
if sum(skill_base) == skill_points:
print("Values stored successfully!")
break
if sum(skill_base) > skill_points:
print("Not valid")
break
skill = int(input(f'skill_ {str(i+1)}: '))
skill_base.append(skill)
if sum(skill_base) == skill_points:
print("Values stored successfully!")

Is there a proper variable to track how many times a loop has looped?

I'm pretty new to coding, so forgive me if this is super obvious.
When running a while loop, and I want to only run if for a certain amount of times. Ex (python):
question_var = 0
while True:
if condition:
question_var += 1
continue
print("Condition not met")
I want to know if there is a proper variable to use in place of question_var. Similar to how i is used to represent index in a for loop. I understand that this is not necessary, but I just want to learn how to do it right. I have been using a variable named rev
Here is the actual program I am trying to run:
rev = 0
while rev <= 10:
new_file_name = "C# is bad ({}).txt".format(rev)
with open(new_file_name, 'w') as tempfile:
tempfile.write("C# is bad.\nPython is good.\n")
rev += 1
its a gag I made for my friend who likes C#, but when I was making it I felt the the variable rev seemed unprofesional.
using a while True loop is useful in some cases, but it is often not the most idiomatic use in python. As mentioned in the comments, you might find a for loop more appropriate:
for rev in range(0, 11):
new_file_name = "C# is bad ({}).txt".format(rev)
with open(new_file_name, 'w') as tempfile:
tempfile.write("C# is bad.\nPython is good.\n")
Also, you don't really need to use format() here, you could change the second line to:
new_file_name = f"C# is bad ({rev}).txt"
The code follows should be a working approach if you're using Python 3.8, but I can't test it currently since I'm on my phone
counter = 0
maxLoops = 10
while (counter := counter++) < maxLoops:
# do some magic
But a better approach is to use a for loop if you want to loop a certain amount.
maxLoops = 10
for i in range(maxLoops):
# do some magic
But there is no convention for how to run a while loop for a certain amount.
For the first part of the question you could use for and break:
need=2
for i in range(0,11):
if i%3 == 0:
print(i)
need -= 1
if need <= 0:
break
print("i is {} after the loop".format(i))
Output:
0
3
i is 3 after the loop
This loop will run at most 11 times, but finishes immediately when it finds the two special values it is looking for. Also, the loop variable remains accessible after the loop. Yeah, Python is different from C# here.
However, and this leads to the second part of the question, not knowing this means that you may rather want to implement something like this:
for rev in range(0, 11):
new_file_name = "Python is new to me ({}).txt".format(rev)
with open(new_file_name, 'w') as tempfile:
tempfile.write("I will learn Python first.\nAnd even then I will not speak nonsense.\n")

Converting Fortran 77 code to Python

I have looked at this Q/A Intent of this Fotran77 code and I have almost converted the below Fortran77 style code into Python 3.x except I had a doubt where the i = i + 1 should be placed in the Python version. As mentioned in the comments of the linked question I have done the conformance tests and the results are off by a margin of 2. Hence the question.
i = 0
500 continue
i = i +1
if (i .le. ni) then
if (u(i,j-1) .gt. -9999.) then
r(1,j) = u(i,j-1)
go to 600
else
missing = i
go to 500
end if
end if
600 continue
Here is my Python version
i = 0
while (i <= ni):
i = i+1
if (u[i,j-1] > -9999.0):
r[0,j] = u[i,j-1]
break
else:
missing = i
Did I place the increment counter at the right location ?
Directly translating is not advised because you loose a number of nice efficient coding features of python.
To do this properly in python you should 1) recognize the 0- index convention of python, and 2 ) recognize that that fortran is column major and python is row major so you should reverse the index ordering for all multi-dimensional arrays.
If you do that the loop can be written:
try:
r[j,0]=[val for val in u[j] if val > -9999 ][0]
missing=False
except:
missing=True
I'm assuming we don't actually need the numeric value of missing.
If you need it you will have something like this:
try:
missing,r[j,0]=[(index,val) for (index,val) in enumerate(u[j]) if val > -9999 ][0]
except:
missing=-1
You could also use next which would be faster, but it gets a little trickier handling the missing condition.

Does return finishes a function on Python?

I'm learning on codeacademy and I was trying to print the following:
def trip_cost(p, d, m):
return rental_car_cost(d) + hotel_cost(d) + plane_ride_cost(p) + m
print "%s to %s for %s days with an extra %s dollars of spending mone" (trip_cost(p, d, m), p, d, m)
The program was telling me nothing was printing on the console, so I proceeded to delete the return line and it worked, so I was wondering if every time a function reaches a return it finishes, in that case in the following code I could've save the "and days < 7" ??
def rental_car_cost(days):
cost = days*40
if days >= 7:
cost -= 50
return cost
elif days >= 3 and days < 7:
cost -= 20
return cost
print cost
so I was wondering if every time a function reaches a return it finishes
Yes, it returns to what called it, hence the name.
in that case in the following code I could've save the and days < 7
Yes. Also you could have left it out because elif means "else if" so even if you didn't return that case would only have been considered if the previous if had been false so that it is considered else.
Generally we call the code and days < 7 here redundant. It's a good idea to remove redundant code. If redundancy makes something clearer then there's little harm leaving it in, but as a rule redundancy is more likely to confuse someone than to assist them, especially when you get more familiar with the language.
Yes, you can leave out and days < 7
Yes, return finishes the function and nothing else after that line is executed. Your example will work fine for the if-elif-else statements, as the appropriate condition will be checked and executed, returning the corresponding cost.
As a tip, you can use elif 3 <= days < 7 instead of elif days >= 3 and days < 7. Python is nice like that! In the way your conditions are structured, you can just have elif days >=3.

Categories

Resources