Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If/else problems
#31
Remember what I told you earlier?

(Dec-21-2017, 11:37 AM)Terafy Wrote: Just create a variable, just under the def a():, called found and set it as false.
If payroll number matches with employee payroll. Print employee details and found is set true.
The else-statement become an if-statement for found is _____.

Don't modify function a(). Just do it to function b() and c()
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#32
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' % (employee[4]+ ','+' '+employee[3]), '%30s' % employee[0], '%30s' % employee[1], '%30s' % employee[2])
            if len(i)==6:
                print('%-30s' % (employee[5]+','+' '+employee[3]+' '+employee[4]), '%30s' % employee[0], '%30s' % employee[1], '%30s' % employee[2])
            if len(i)==7:
                print('%-30s' % (employee[6]+','+' '+employee[3]+' '+employee[4]+' '+employee[5]), '%30s' % employee[0], '%30s' % employee[1], '%30s' % employee[2])
    else:
        print("There are no employees within that salary range")
Output:
Select an option from 1 - 3 (0 to quit): 2 Enter the minimum salary: 10000 Enter the maximum salary: 40000 Flanders, Ned 12346 25000 Teacher Simpson, Lisa 12347 20000 Secretary Granger, Hermione 12348 20000 Wizard Potter, Harry 12349 30000 Wizard Shmoikel, Herschel 12350 15000 Entertainer T, Mike 13126 35000 Lecturer Flintstone, Wilma 13130 32000 Secretary There are no employees within that salary range
def c():
    jobs = input("Enter the job title: ")
    if jobs in employees:
        if len(i)==5:
                print('%-30s' % (jobs[4]+ ','+' '+jobs[3]))
        if len(i)==6:
                print('%-30s' % (jobs[5]+','+' '+jobs[3]+' '+jobs[4]))
        if len(i)==7:
                print('%-30s' % (jobs[6]+','+' '+jobs[3]+' '+jobs[4]+' '+jobs[5]))
    else:
        print("There is no such job")
Output:
Select an option from 1 - 3 (0 to quit): 3 Enter the job title: wizard There is no such job
I didn't fully get how to use the found thing. Lemme see.
Reply
#33
def b():
    salmin = int(input("Enter the minimum salary: "))
    salmax = int(input("Enter the maximum salary: "))
    found = False
    for employee in employees:
        if salmin <= int(employee[1]) <= salmax:
            if len(employee)==5:
                print('%-30s' % (employee[4]+ ','+' '+employee[3]), '%30s' % employee[0], '%30s' % employee[1], '%30s' % employee[2])
                found = True
            if len(employee)==6:
                print('%-30s' % (employee[5]+','+' '+employee[3]+' '+employee[4]), '%30s' % employee[0], '%30s' % employee[1], '%30s' % i[2])
                found = True
            if len(employee)==7:
                print('%-30s' % (employee[6]+','+' '+employee[3]+' '+employee[4]+' '+employee[5]), '%30s' % employee[0], '%30s' % i[1], '%30s' % i[2])
                found = True
    if found == False:
            print("There are no employees within that salary range")
def c():
    found = False
    search = input("Enter the job title: ")
    for jobs in employees:
        if search == jobs[2]:
            print(jobs[3]+","+ jobs[4])
            found = True
    if found == False:
        print("There is no such job")

whoo... 1 min to spare
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#34
Ok b works now, but c is just selecting all names. Am I using the right variable? I'm using jobs, not a previous one that's already assigned to something else.

def c():
    found = False
    jobs = input("Enter the job title: ")
    for jobs in employees:
        if len(i)==5:
                print('%-30s' % (jobs[4]+ ','+' '+jobs[3]))
                found = True
        if len(i)==6:
                print('%-30s' % (jobs[5]+','+' '+jobs[3]+' '+jobs[4]))
                found = True
        if len(i)==7:
                print('%-30s' % (jobs[6]+','+' '+jobs[3]+' '+jobs[4]+' '+jobs[5]))
                found = True
    if found == False:
        print("There is no such job")
Output:
Select an option from 1 - 3 (0 to quit): 3 Enter the job title: wizard Simpson, Bart Flanders, Ned Simpson, Lisa Granger, Hermione Potter, Harry Shmoikel, Herschel Ibrahimovic, Zlatan Southgate, Gareth Klopp, Juergen T, Mike Laurie, Adele Capaldi, Peter Christopher, Edward Flintstone, Wilma

1 min to spare?
Reply
#35
you store the input into the variable jobs in line 3 and then override the variable jobs with the for loop in line 4.

your if-statement also have the variable i

(Dec-21-2017, 01:05 PM)Skelbo Wrote: 1 min to spare?
haha that was the 5 hour mark Cool
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#36
You did quite a lot of progress while I was away (train travel)!
Reply
#37
Ok what am I doing wrong now.

def c():
    found = False
    jobs = input("Enter the job title: ")
    for b in employees:
        if jobs == b[2]:
            if len(i)==5:
                print('%-30s' % (b[4]+ ','+' '+b[3]))
                found = True
            if len(i)==6:
                print('%-30s' % (b[5]+','+' '+b[3]+' '+b[4]))
                found = True
            if len(i)==7:
                print('%-30s' % (b[6]+','+' '+b[3]+' '+b[4]+' '+b[5]))
                found = True
    if found == False:
        print("There is no such job")
Output:
Select an option from 1 - 3 (0 to quit): 3 Enter the job title: wizard There is no such job
Reply
#38
I think wizard is NOT equal to Wizard. Do you see why? wwwWWWwww
Reply
#39
(Dec-21-2017, 05:02 PM)squenson Wrote: You did quite a lot of progress while I was away (train travel)!

I tried to help him finish on time... but we missed the deadline. Sad

def c():
    found = False
    jobs = input("Enter the job title: ")
    for b in employees:
        if jobs == b[2]:
            
            ## what with the len(i) in these if-statement
            if len(i)==5:
                print('%-30s' % (b[4]+ ','+' '+b[3]))
                found = True
            if len(i)==6:
                print('%-30s' % (b[5]+','+' '+b[3]+' '+b[4]))
                found = True
            if len(i)==7: 
                print('%-30s' % (b[6]+','+' '+b[3]+' '+b[4]+' '+b[5]))
                found = True
    if found == False:
        print("There is no such job")
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#40
Ok got it. Replaced the len(i) with len(b). I thought I had to refer to the previously defined i. I keep getting confused about that.
Is there any way to have the program detect lowercase jobs too? So that if someone enters wizard it matches it with Wizard anyway?

Also I had a second part of the assignment which was much harder and I tried researching and asking on another site about it alongside. It pretty much failed too. But I'm curious to see what was wrong. Can I paste it here or should I open a new thread?
Reply


Forum Jump:

User Panel Messages

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