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


Messages In This Thread
If/else problems - by Skelbo - Dec-21-2017, 07:39 AM
RE: If/else problems - by squenson - Dec-21-2017, 08:10 AM
RE: If/else problems - by Skelbo - Dec-21-2017, 08:44 AM
RE: If/else problems - by squenson - Dec-21-2017, 08:50 AM
RE: If/else problems - by Skelbo - Dec-21-2017, 09:03 AM
RE: If/else problems - by squenson - Dec-21-2017, 09:14 AM
RE: If/else problems - by Skelbo - Dec-21-2017, 09:26 AM
RE: If/else problems - by squenson - Dec-21-2017, 09:43 AM
RE: If/else problems - by Skelbo - Dec-21-2017, 09:58 AM
RE: If/else problems - by squenson - Dec-21-2017, 10:26 AM
RE: If/else problems - by Skelbo - Dec-21-2017, 10:34 AM
RE: If/else problems - by Terafy - Dec-21-2017, 10:41 AM
RE: If/else problems - by Skelbo - Dec-21-2017, 10:49 AM
RE: If/else problems - by Terafy - Dec-21-2017, 11:00 AM
RE: If/else problems - by Skelbo - Dec-21-2017, 11:12 AM
RE: If/else problems - by Terafy - Dec-21-2017, 11:17 AM
RE: If/else problems - by Skelbo - Dec-21-2017, 11:31 AM
RE: If/else problems - by squenson - Dec-21-2017, 11:36 AM
RE: If/else problems - by Terafy - Dec-21-2017, 11:37 AM
RE: If/else problems - by Terafy - Dec-21-2017, 12:49 PM
RE: If/else problems - by Skelbo - Dec-21-2017, 11:42 AM
RE: If/else problems - by Terafy - Dec-21-2017, 11:54 AM
RE: If/else problems - by Skelbo - Dec-21-2017, 12:00 PM
RE: If/else problems - by Terafy - Dec-21-2017, 12:06 PM
RE: If/else problems - by Skelbo - Dec-21-2017, 12:16 PM
RE: If/else problems - by Terafy - Dec-21-2017, 12:22 PM
RE: If/else problems - by Skelbo - Dec-21-2017, 12:22 PM
RE: If/else problems - by Terafy - Dec-21-2017, 12:30 PM
RE: If/else problems - by Skelbo - Dec-21-2017, 12:34 PM
RE: If/else problems - by Terafy - Dec-21-2017, 12:35 PM
RE: If/else problems - by Skelbo - Dec-21-2017, 12:40 PM
RE: If/else problems - by Skelbo - Dec-21-2017, 12:56 PM
RE: If/else problems - by Terafy - Dec-21-2017, 12:59 PM
RE: If/else problems - by Skelbo - Dec-21-2017, 01:05 PM
RE: If/else problems - by Terafy - Dec-21-2017, 01:08 PM
RE: If/else problems - by squenson - Dec-21-2017, 05:02 PM
RE: If/else problems - by Terafy - Dec-21-2017, 10:35 PM
RE: If/else problems - by Skelbo - Dec-21-2017, 05:43 PM
RE: If/else problems - by squenson - Dec-21-2017, 07:41 PM
RE: If/else problems - by Skelbo - Dec-22-2017, 12:08 PM
RE: If/else problems - by Terafy - Dec-22-2017, 12:25 PM
RE: If/else problems - by Skelbo - Dec-22-2017, 12:31 PM
RE: If/else problems - by Terafy - Dec-22-2017, 12:35 PM
RE: If/else problems - by squenson - Dec-22-2017, 12:39 PM
RE: If/else problems - by Skelbo - Dec-22-2017, 12:40 PM
RE: If/else problems - by squenson - Dec-22-2017, 12:44 PM
RE: If/else problems - by Skelbo - Dec-22-2017, 12:47 PM
RE: If/else problems - by Terafy - Dec-22-2017, 12:51 PM
RE: If/else problems - by Skelbo - Dec-22-2017, 01:02 PM

Forum Jump:

User Panel Messages

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