Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If/else problems
#1
Got an assignment due in 5 hours and I'll most likely fail it of course. Been at it for days on end and I can't figure out what I'm doing wrong. Quick description of (this part of) the assignment:

In the main body of the program, write code which prompts the user for a filename and attempts to open the file whose name is supplied. If the file cannot be opened an error message should be output and the program should terminate; otherwise the program should read each line from the file and supply it to the employee tuple-creation function, storing the tuples returned by this function in a list. You may assume that the file contains lines that conform to the format described for the argument to the function.
After the data has been input the program should display details of all the employees in the list in a neat table (using the function already written) then enter a loop in which the user should be given the option of requesting the display of full details the employee with a given payroll number or of all employees with a salary in a particular range (e.g. 20000 to 30000) or the first and last names of all employees with a specified job title using the format Bartholomew Simpson (other names, when they exist, must not be output) or quitting the program. The user should then be asked to supply the job title, payroll number or lower and upper bounds of the salary range (as appropriate), the list should then be searched and the appropriate output displayed (using the function written earlier when full details are required). The salary output should be sorted numerically in ascending order of salary.. Appropriate messages should be displayed if a search produces no results. The user should be told what has to be typed in order to select each option; the input options should be concise (e.g. ‘1’, ‘2’ etc. or ‘j’, ‘s’ etc.) in order to allow quick testing of the program.

My code:
payroll = int(input("Enter the payroll number: "))
salary = int(input("Enter the salary: "))
job = input("Enter the job title: ")
name = input("Enter the first name(s): ")
surname = input("Enter the surname: ")

print(surname+",", name, '%10s' % payroll, '%10s' % job, '%10s' % salary)

file = open(input("Enter the name of the file: "), "r").readlines()

employees=[]
for line in file:
    employees.append(line.split())

for i in employees:               
    if len(i)==5:
        print('%-30s' % (i[4]+ ','+' '+i[3]), '%30s' % i[0], '%30s' % i[1], '%30s' % i[2])
    if len(i)==6:
        print('%-30s' % (i[5]+','+' '+i[3]+' '+i[4]), '%30s' % i[0], '%30s' % i[1], '%30s' % i[2])
    if len(i)==7:
        print('%-30s' % (i[6]+','+' '+i[3]+' '+i[4]+' '+i[5]), '%30s' % i[0], '%30s' % i[1], '%30s' % i[2])

def a():
    pr = int(input("Enter the payroll number: "))
    for a in employees:
        if a in employees:                
            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")
     
def b():
    salmin = int(input("Enter the minimum salary: "))
    salmax = int(input("Enter the maximum salary: "))
    for employee in employees:
        if salmin <= int(employee[1]) <= salmax:
            if len(i)==5:
                print('%-30s' % (i[4]+ ','+' '+i[3]), '%30s' % i[0], '%30s' % i[1], '%30s' % i[2])
            if len(i)==6:
                print('%-30s' % (i[5]+','+' '+i[3]+' '+i[4]), '%30s' % i[0], '%30s' % i[1], '%30s' % i[2])
            if len(i)==7:
                print('%-30s' % (i[6]+','+' '+i[3]+' '+i[4]+' '+i[5]), '%30s' % i[0], '%30s' % i[1], '%30s' % i[2])
            return employee
    else:
        print("There are no employees within that salary range")

def c():
    jobs = input("Enter the job title: ")
    if jobs in employees:
        print(employees.surname+",", employees.name)
    else:
        print("There is no such job")
    
print("Here are your options:")
print("1. Full details of an employee with a given payroll number")
print("2. Full details of all employees with a salary in selected range")
print("3. The first and last name of employees with a selected job")
menu = [None, a, b, c]
running = True
while running :
    line = input("Select an option from 1 - 3 (0 to quit): ")
    if line == "0" : running = False
    elif len(line)==1 and "1"<=line<="3": menu[int(line)]()
    else : print("Invalid input. Please try again: ")
My problem lies with the queries a, b, c. They have various errors. I tried different ways of writing them but none worked fully. Example:
Output:
Select an option from 1 - 3 (0 to quit): 1 Enter the payroll number: 12346 Select an option from 1 - 3 (0 to quit):
For a()(aka 1 in menu selection), I tried different solutions. One resulted in printing the else statement several times and the other which I thought was better (posted here) just returned me to the menu.

Output:
Select an option from 1 - 3 (0 to quit): 2 Enter the minimum salary: 10000 Enter the maximum salary: 30000 Flintstone, Wilma 13130 32000 Secretary
For b() aka 2 in menu selection - it is not returning all the rows that fit the if statement requirement.

Output:
Select an option from 1 - 3 (0 to quit): 3 Enter the job title: wizard There is no such job
And for c() aka 3 in menu - it's only outputting the else statement even if the job input is valid and should identify a row.
Reply
#2
Let's have a look at a():
1. On line 25, you loop through each employee, then on line 26 you check whether the selected employee is in employee, I don't think this is necessary!
2. Look at line 27. I guess you want to compare the employee number you have entered with the employee number of the selected employee. Which variable stores the selected employee, is it i or ...?
3. After you have found a match between the pr number and the selected employee number, you print the result, then do you really need to continue your search?
Reply
#3
1. It was a desperate attempt to try stuff xD I shall delete if not needed
2. Yes I'm trying to compare the user input payroll with the one present in the list. I thought since I previously identified the position in the list for the payroll as i[0] I can refer to it later? Do I have to save each position into variables and then refer to the variable name?
3. I don't know how to stop the search. I used return (whatevervalue) to try and stop the search. I don't know any other way.
Reply
#4
1. Good, delete it, it is useless
2. Another hint: compare the variable you use in line 25 and the one you use in line 27. Is this logical?
3. return is an excellent way to stop a for loop. It can be used without returning any value. Where would you place it?
Reply
#5
for i in employees:               
    if len(i)==5:
        print('%-30s' % (i[4]+ ','+' '+i[3]), '%30s' % i[0], '%30s' % i[1], '%30s' % i[2])
        payroll=i[0]
        salary=i[1]
        job=i[2]
        name1=i[3]
        surname=i[4]
    if len(i)==6:
        print('%-30s' % (i[5]+','+' '+i[3]+' '+i[4]), '%30s' % i[0], '%30s' % i[1], '%30s' % i[2])
        payroll=i[0]
        salary=i[1]
        job=i[2]
        name1=i[3]
        name2=i[4]
        surname=i[5]
    if len(i)==7:
        print('%-30s' % (i[6]+','+' '+i[3]+' '+i[4]+' '+i[5]), '%30s' % i[0], '%30s' % i[1], '%30s' % i[2])
        payroll=i[0]
        salary=i[1]
        job=i[2]
        name1=i[3]
        name2=i[4]
        name3=i[5]
        surname=i[6]
    
def a():
    pr = int(input("Enter the payroll number: "))
    for a in employees:
        if pr==payroll:
            print('%-30s' % (i[4]+ ','+' '+i[3]), '%30s' % i[0], '%30s' % i[1], '%30s' % i[2])
            return a
        else:
            print("There is no such employee")
Output:
Enter the payroll number: 12346 There is no such employee There is no such employee There is no such employee There is no such employee There is no such employee There is no such employee There is no such employee There is no such employee There is no such employee There is no such employee There is no such employee There is no such employee There is no such employee There is no such employee
:(
I used pr because that's the variable I saved the user input into. So I'm comparing that variable now to the newly saved payroll variable from position i[0] from previous part.
Reply
#6
2. You continue to compare pr with i[0] (you called it payroll). i is NOT the correct variable to use in line 30 and 31. What does i contain? Insert a print(i) right at the beginning of a().
4. Your block 33/34 is NOT properly indented, this is why you get the output you mentioned.
Reply
#7
2. Ok printing i is showing the last row details. I'm really out of ideas of what variable I'm supposed to compare to. I don't know how else to identify parts of the list other than by positions or by slices or variables.
4. I thought if and else had to have the same indentation?
Reply
#8
2. Let me write it in plain English, you translate it in python:
if pr is equal to the selected employee's element 0

4. Correct, if and else have to be aligned but in this case the else is not for the if block, but for the for loop...
Reply
#9
if pr==a[0]
?
Reply
#10
Looks good try it...
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020