+ 1

Working with if-else statements

This is part of an assignment I have due tomorrow. Not sure what I'm doing wrong, because it won't recognize the if statements at all, and instead just take the 'else' action even if the input is valid and should in fact return the if action. Here's the code: def a(): for i in employees: pr = int(input("Enter the payroll number: ")) if pr==i[0]: print('%-30s' % (i[4]+ ','+' '+i[3]), '%30s' % i[0], '%30s' % i[1], '%30s' % i[2]) else: print("There is no such employee") #there's a list with employee names, surnames, payrolls, salary and job title. Can paste the whole thing if needed. Over here I am trying to say if the user input value matches the value at position 0 in the list (which is the payroll), then type the details of that row. It's only outputting "there is no such employee". def b(): salmin = int(input("Enter the minimum salary: ")) salmax = int(input("Enter the maximum salary: ")) salaryrange = salmin<=salary<=salmax if salaryrange in employees: print("test") else: print("There are no employees within that salary range") #trying to select and display the details of the employees within a salary range. Again, only outputs else statement. def c(): jobs = input("Enter the job title: ") while jobs in employees: print(surname+",", name) else: print("There is no such job") #trying to say if the user input value exists in the list, then print the selected details of the identified rows matching the value input. Only outputs "there is no such job". What exactly am I doing wrong? EDIT: Never used playground before, didn't know you could save and link code. Here's the link (I think): https://code.sololearn.com/cjJ8vmpf7uDD/#py

20th Dec 2017, 6:50 AM
Laura
Laura - avatar
9 Respuestas
+ 10
You'll need a flag/variable to keep track whether the element was found after the loop. If it's found then you can print it accordingly and break the loop as we don't need to continue searching. Otherwise if we have finished looping but still couldn't find the record, then only we will print no record found. Hopefully it helps! 😉
20th Dec 2017, 7:20 AM
Zephyr Koo
Zephyr Koo - avatar
+ 9
Thanks for your update. I believe the loop @ Line 24 was used for searching right? If it's the case we should move the search criteria from Line 25 to outside of the loop as its value was used for every iteration.
20th Dec 2017, 7:11 AM
Zephyr Koo
Zephyr Koo - avatar
+ 7
If possible, please provide a Minimal, Complete, and Verifiable code @ Code Playground so we can help you better. 😉
20th Dec 2017, 6:34 AM
Zephyr Koo
Zephyr Koo - avatar
+ 1
Added a link
20th Dec 2017, 6:51 AM
Laura
Laura - avatar
0
in c(), you have an else statement without an if statement before it. Other than that, I can't find any other problems without all the data being used.
20th Dec 2017, 6:35 AM
Cailyn Baksh
0
Like this? I switched it with line 24 but it's not working still.
20th Dec 2017, 7:16 AM
Laura
Laura - avatar
0
It's confusing still. What do you mean by flag/variable? I'm used to for loops stopping when the result was found using the same type of code and indentation. Where am I supposed to shove a variable? How is that supposed to check if the value has been found? Isn't the line at 26 doing exactly that? It's meant to say if value is found, then print this.
20th Dec 2017, 7:40 AM
Laura
Laura - avatar
0
tmyiy it
20th Dec 2017, 1:29 PM
atprana
atprana - avatar
0
Updated the code with the advice you gave, storing the found element in a variable and then attempting to print it... still getting the same result. Could you be more specific? I'm seriously very new at python. I need crystal clear instructions.
20th Dec 2017, 4:41 PM
Laura
Laura - avatar