views:

286

answers:

2

I am only going to post the portion where the problem is at, the program has no error (all the codes are valid except for this raw_input problem)

I tested with search_function(1) and etc and it worked.

But if I do this while loop, it doesn't print anything. Example output:

Enter a number to print specific table, or STOP to quit: 2 Enter a number to print specific table, or STOP to quit: 2 Enter a number to print specific table, or STOP to quit: 1 Enter a number to print specific table, or STOP to quit: Enter a number to print specific table, or STOP to quit: 1 Enter a number to print specific table, or STOP to quit: Enter a number to print specific table, or STOP to quit: STOP

def search_function(x):
    if x == 1:
        for student in students:
            print "%-17s|%-10s|%-6s|%3s" % student.print_information()
        print '\n'

    if x == 2:
        print "%-17s|%-10s|%s" %(header[0],header[1],header[4])
        print "-" * 45
        for student in students:
            print "%-17s|%-10s|%s" %student.print_first()
        print '\n'
        print "Simple Analysis on favorite sports: "
        # Printing all sports that are specified by students
        for s in set(Student.sports): # class attribute
            print s, Student.sports.count(s), round(((float(Student.sports.count(s)) / num_students) *100),1)

        # Printing sports that are not picked 
        allsports = ['Basketball','Football','Other','Baseball','Handball','Soccer','Volleyball','I do not like sport']
        for s in set(allsports) - set(Student.sports):
            print s, 0, '0%'
        choice_list = Student.sports
        for choice in choice_list:
            choice_dict[choice] = choice_dict.get(choice, 0) + 1
        print max(choice_dict)
        print min(choice_dict)

    elif x == 3:
        print "%-17|%-10s|%-16s|%s" %(header[0],header[1],header[5],header[6])
        print "-" * 45
        for student in students:
            print "%-17s|%-10s|%-16s|%s" % student.print_second()
        print '\n'

    elif x == 4:
        print "%-17s|%-10s|%s" %(header[0],header[1],header[7])
        print "-" * 45
        for student in students:
            print "%-17s|%-10s|%s" %student.print_third()
        print '\n'

    elif x == 5:
        print "%-17s|%-10s|%-15s|%s" %(header[0],header[1],header[8],header[9])
        print "-" * 45
        for student in students:
            print "%-17s|%-10s|%-16s|%s" % student.print_fourth()
        print '\n'

x = raw_input("Enter a number to print specific table, or STOP to quit: ")
while x != 'STOP':
    search_function(x)
    x = raw_input("Enter a number to print specific table, or STOP to quit: ")
+2  A: 

raw_input() returns string, while your code expects integer. Use search_function(int(x)) or change conditions to compare with strings.

Denis Otkidach
Thank you for the tip. However, one reason to use raw is to recognize the input STOP, but because it is int, it simply returns an error. It is fine for my purpose but just want to know, is there any alternative to this?I can create an extra if/else statement, but please int, will just return error before it hits my false statement
JohnWong
Yes, there is alternative (quoting): "or change conditions to compare with strings", i.e. change `x == 1` to `x == '1'` etc. But passing `int` into `search_function()` won't cause an error, since comparisson with `'STOP'` is outside function.
Denis Otkidach
uhhh good idea. :) thanks a lot
JohnWong
+1  A: 

Test for x == 'STOP' first and break if True, else cast to int and call search_function:

while True:
    x = raw_input("Enter a number to print specific table, or STOP to quit: ")
    if x == 'STOP':
        break
    search_function(int(x))
jellybean
thanks, i used it. it was good
JohnWong