Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If/else problems
#11
Output:
Select an option from 1 - 3 (0 to quit): 1 Enter the payroll number: 12346 There is no such employee
I changed indentation too. Shouldn't be the problem.

def a():
    pr = int(input("Enter the payroll number: "))
    for a in employees:
        if pr==a[0]:
            print('%-30s' % (i[4]+ ','+' '+i[3]), '%30s' % i[0], '%30s' % i[1], '%30s' % i[2])
            return a
    else:
        print("There is no such employee")
Reply
#12
The if-statement and the else-statement indent isn't matching.

also what type of data is employees? could you type print(employees).
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#13
I was told it shouldn't match in this case. If it matches it just prints the else statement several times.
Output:
Select an option from 1 - 3 (0 to quit): 1 Enter the payroll number: 12345 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
print(employees)
Output:
[['12345', '55000', 'Consultant', 'Bart', 'Simpson'], ['12346', '25000', 'Teacher', 'Ned', 'Flanders'], ['12347', '20000', 'Secretary', 'Lisa', 'Simpson'], ['12348', '20000', 'Wizard', 'Hermione', 'Granger'], ['12349', '30000', 'Wizard', 'Harry', 'Potter'], ['12350', '15000', 'Entertainer', 'Herschel', 'Shmoikel', 'Krustofski'], ['13123', '75000', 'Consultant', 'Zlatan', 'Ibrahimovic'], ['13124', '150000', 'Manager', 'Gareth', 'Southgate'], ['13125', '75000', 'Manager', 'Juergen', 'Klopp'], ['13126', '35000', 'Lecturer', 'Mike', 'T', 'Sanderson'], ['13127', '200000', 'Entertainer', 'Adele', 'Laurie', 'Blue', 'Adkins'], ['13128', '50', 'Timelord', 'Peter', 'Capaldi'], ['13129', '250000', 'Entertainer', 'Edward', 'Christopher', 'Sheeran'], ['13130', '32000', 'Secretary', 'Wilma', 'Flintstone']]
Reply
#14
So,
if pr == a[0]
The if-statement would flag a false for everything because you are comparing an integer variable to a string variable.

You need to change it so that it would either compare string with string or integer with integer
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#15
Hm, I thought if the value from the list was an int then it would match. So I have to use
int(a[0])
then? I tried it and...
Output:
Enter the payroll number: 12349 There is no such employee There is no such employee There is no such employee There is no such employee Flintstone, Wilma 13130 32000 Secretary Select an option from 1 - 3 (0 to quit): 1 Enter the payroll number: 12345 Flintstone, Wilma 13130 32000 Secretary Select an option from 1 - 3 (0 to quit): 1 Enter the payroll number: 13128 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 Flintstone, Wilma 13130 32000 Secretary
Gvarious weird results with each different number I put in. I'm lost.

Edit: the above output was when if/else indentation matched. Forgot to change since I switched to show example. The actual error output is just wilma row no matter what pr value i input.

Output:
Enter the payroll number: 12345 Flintstone, Wilma 13130 32000 Secretary Select an option from 1 - 3 (0 to quit): 1 Enter the payroll number: 13126 Flintstone, Wilma 13130 32000 Secretary Select an option from 1 - 3 (0 to quit): 1 Enter the payroll number: 13124 Flintstone, Wilma

Any feedback on the other two that give errors? b and c?
Reply
#16
Yeah, your code does this this:
If payroll number matches with employee payroll. Print employee details.
If payroll number does not match with employee payroll. Print "There is no such employee".

what you should of done:
If payroll number matches with employee payroll. Print employee details.
If there payroll does not match with ANY employee payroll. Print "There is no such employee".
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#17
I don't know how to do that :/

How can I say if it doesn't match with ANY employee. I thought referring to a[0] would refer to all the values in all rows that have element 0.
Reply
#18
One way to exit a() is to find a match. But suppose there is no match, the for loop will end and then you can print 'not found'.
Reply
#19
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 _____.
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#20
def a():
    found = False
    pr = int(input("Enter the payroll number: "))
    for a in employees:
        if pr==int(a[0]):
            print('%-30s' % (i[4]+ ','+' '+i[3]), '%30s' % i[0], '%30s' % i[1], '%30s' % i[2])
            found = True
            return a
        elif pr!=int(a[0]):
            found = False
            print("There is no such employee")
Still same row.
Reply


Forum Jump:

User Panel Messages

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