Reputation: 61
I am doing an assignment for class where you gather the salary of your employees and I have become stumped. It keeps continuously looping the ""What is their salary: "" part so how can I fix it to where the salary matches up with the employee, instead of it keep on asking what the salary is? This is what I have so far please help me out with any tips advice. (I am not asking for you to do my work. I just want some tips/advice)
def main():
name = []
salary = []
n = 0
n = int(input("How many employee's will you be entering?: "))
for i in range(n):
name = input("Enter the employees name: ")
empsalary(name,salary)
def empsalary(empname,esalary):
salary = 0
salary = int(input("What is their salary: "))
for i in range(salary):
if salary < 1 or salary > 200000:
salary = int(input("What is their salary: "))
if salary > 0 and salary < 200000:
salary = int(input("What is their salary: "))
Upvotes: 0
Views: 144
Reputation: 49816
This line does not do what you think it does:
for i in range(salary):
This will iterate salary
times, sequentially assigning i
the values 0
to salary-1
.
That is why it keeps looping over the input in the body of that loop. It also doesn't matter if you change salary
in the body, because range(salary)
is evaluated on entry to the loop, and evaluates to a list containing the numbers which will be assigned to i
.
Update:
Also, there is no value of salary
that will not trigger one of these if clauses;
if salary < 1 or salary > 200000:
salary = int(input("What is their salary: "))
if salary > 0 and salary < 200000:
salary = int(input("What is their salary: "))
Update 2:
Look at this code:
for i in range(n):
name = input("Enter the employees name: ")
empsalary(name,salary)
You call empsalary
only once, after asking for all employee names. I can tell this is not what you want.
Update 3: I also note that you don't actually do anything with the name
s you capture. You just overwrite them in the next iteration. Don't do that, store them.
Update 4: You also don't do anything with the salary
s , and you don't actually return anything from your function.
Upvotes: 1
Reputation: 6541
Let's try something more like this:
def main():
name = []
salary = []
n = 0
n = int(input("How many employee's will you be entering?: "))
for i in range(n):
name.append( input("Enter the employees name: ") )
salary.append( int(input("What is their salary: ")) )
print_high_and_low(name, salary)
def print_high_and_low(name, salary):
"""Find maximum and minimum salaries"""
# find maximum salary
max = 0
for i in range(len(salary)):
if salary[i] > max:
max = salary[i]
max_index = i
# find minimum salary
min = 9999999 # Smarter would be: sys.maxint (if you import sys)
for i in range(len(salary)):
if salary[i] < min:
min = salary[i]
min_index = i
print name[max_index] + " has the highest salary of: " + str(max)
print name[min_index] + " has the lowest salary of: " + str(min)
Notice that I did not need the empsalary
method.
Issues with the original version:
empsalary
method your loop for i in range(salary)
loops over the SALARY of your latest employee. I think you want to loop over the NUMBER of employees. Can you see that is what I did?name
and salary
as lists, but you weren't treating them like lists. In this version you will save all the data entered in two lists for later use (finding the max and min salaries).Upvotes: 1
Reputation: 1
I am no expert in Python, but I think I see what your problem is.
According to your code here, you're asking the user for a number of any size. Once they've input this number, then program proceeds to execute a FOR loop [salary] number of times. This can be a problem if you've made the employee's salary something huge like 10,000, as this will cause the loop to execute 10,000 times!
I'm guessing that you're trying to make sure that the salary is somewhere between 0 and 200,000 and if the salary falls outside of this range, then the program will continue to ask for a salary. If this is indeed the case, a while loop should do the trick while eliminating a couple lines of code.
def empsalary(empname,esalary):
salary = 0
salary = int(input("What is their salary: "))
while (salary < 1 or salary > 200000):
salary = int(input("What is their salary: "))
This should do what you need. Good luck on your assignment!
Upvotes: 0