Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If/else problems
#21
Haha... if your using squenson method you don't need my one. (Curse that handsome devil)

squenson method
def a():
    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])
            return
        else:
            print("There is no such employee")
Also I don't like line: 6 you have a variable "i[]" but what is that to the function?... print(i)
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#22
I don't understand either method. And my time is up. Deadline gone. I failed xD

I need some serious help. Lecture slides and lecturer are useless.
Reply
#23
Eh thought you had 1 more hours. You should always check your variable. The variable "i" was used in the most left indent. This was consider a global variable to the function. If you print(i) that was the last data that was used. It should of been:
print('%-30s' % (a[4]+ ','+' '+a[3]), '%30s' % a[0], '%30s' % a[1], '%30s' % a[2])
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#24
Fml I could have had something work if I knew this 15 min earlier
Reply
#25
Might as well finish it off.
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#26
Any help on the other two? I still want to know how it's done even if I'm not getting marks.
Reply
#27
b():
You have the same problem as a(). The issue with i[] needs to be change to the correct variable. Also squenson method was fine with the function a() with just the 1 result. However with multiple results, you needed my method because it check through the entire list without exiting the function.
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#28
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])
            return employee
    else:
        print("There are no employees within that salary range")
I've changed variable and it's being weird again.
Output:
Select an option from 1 - 3 (0 to quit): 2 Enter the minimum salary: 10000 Enter the maximum salary: 30000 Flanders, Ned 12346 25000 Teacher Select an option from 1 - 3 (0 to quit): 2 Enter the minimum salary: 50 Enter the maximum salary: 50000 Flanders, Ned 12346 25000 Teacher
Reply
#29
Thats because you have a "return employee" which kills the function.

You can't use squenson method here.
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#30
Alright! Works, but it's still printing the else statement at the end. Do I have the wrong indentation again? I tried to align it with for and with if as well, they both show it.
Reply


Forum Jump:

User Panel Messages

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