431,900 Members | 1,078 Online
Need help? Post your question and get tips & solutions from a community of 431,900 IT Pros & Developers. It's quick & easy.

 P: 5 I'm using python to write a program that, when given a list of random numbers, sorts them from lowest to highest then it asks the user for a number and it checks that number's place in the sorted list if the number is present, but i keep getting a syntax error on an "if" statement line near the end. is it because i'm not allowed to look up a number in the sorted list? here's my program with a list of example numbers (highlighted in bold is where i get the syntax error): Expand|Select|Wrap|Line Numbers import random def sort_array2_4():     unsorted_array = random.sample(xrange(1000), 10) a=[93,84,200,513,46,1,45,334,92,96,156,82,92,46,] def bubblesort(a):     for swap in range(len(a)-1,0,-1):          for index in range(swap)              if a[index] > a[index + 1]:                a[index], a[index + 1] = a[index + 1], a[index]     return a while(more==True):     num=str(input("Enter a number to search for in list.\n")     if (num in bubblesort(a)):         print num + "is in list at number: ", bubblesort(a).index(num)     else:         print num + "is not recognized in list" Dec 9 '06 #1
8 Replies

 P: 5 I'm using python to write a program that, when given a list of random numbers, sorts them from lowest to highest then it asks the user for a number and it checks that number's place in the sorted list if the number is present, but i keep getting a syntax error on an "if" statement line near the end. is it because i'm not allowed to look up a number in the sorted list? here's my program with a list of example numbers (highlighted in bold is where i get the syntax error): import random def sort_array2_4(): unsorted_array = random.sample(xrange(1000), 10) a=[93,84,200,513,46,1,45,334,92,96,156,82,92,46,] def bubblesort(a): for swap in range(len(a)-1,0,-1): for index in range(swap) if a[index] > a[index + 1]: a[index], a[index + 1] = a[index + 1], a[index] return a while(more==True): num=str(input("Enter a number to search for in list.\n") if (num in bubblesort(a)): print num + "is in list at number: ", bubblesort(a).index(num) else: print num + "is not recognized in list" I'm pretty sure i have all of my indents in the right place, but i can't show you here. Dec 9 '06 #2

 Expert 5K+ P: 6,596 I'm pretty sure i have all of my indents in the right place, but i can't show you here. If fixed that. Not just because I can... You'll learn to use code tags as you go along. The instructions are in the "Posting Guidelines" panel and sticky. Dec 9 '06 #3

 P: 5 If fixed that. Not just because I can... You learn to use code tags as you go along. The instructions are in the "Posting Guidelines" panel and sticky. thanks man, so any ideas on my problem? Dec 9 '06 #4

 Expert 5K+ P: 6,596 Great job! you were very close. next time include error message, ok? Expand|Select|Wrap|Line Numbers import random def sort_array2_4():     unsorted_array = random.sample(xrange(1000), 10) a=[93,84,200,513,46,1,45,334,92,96,156,82,92,46,] def bubblesort(a):     for swap in range(len(a)-1,0,-1):          for index in range(swap):              if a[index] > a[index + 1]:                a[index], a[index + 1] = a[index + 1], a[index]     return a more = True  # must assign before reference while(more==True):     num=input("Enter a number to search for in list.\n")     # one too few parens caused syntax error no next line     # need the int anyway     b = bubblesort(a)  # use lots of variables for debugging     print b   # debug "trap"     if (num in b):         # print will convert to printable, so you don't have to         print num, "is in list at number: ", a.index(num)     else:         print num, "is not recognized in list" Dec 9 '06 #5

 P: 5 Great job! you were very close. next time include error message, ok? Expand|Select|Wrap|Line Numbers import random def sort_array2_4():     unsorted_array = random.sample(xrange(1000), 10) a=[93,84,200,513,46,1,45,334,92,96,156,82,92,46,] def bubblesort(a):     for swap in range(len(a)-1,0,-1):          for index in range(swap):              if a[index] > a[index + 1]:                a[index], a[index + 1] = a[index + 1], a[index]     return a more = True  # must assign before reference while(more==True):     num=input("Enter a number to search for in list.\n")     # one too few parens caused syntax error no next line     # need the int anyway     b = bubblesort(a)  # use lots of variables for debugging     print b   # debug "trap"     if (num in b):         # print will convert to printable, so you don't have to         print num, "is in list at number: ", a.index(num)     else:         print num, "is not recognized in list" Oh my god, thanks a ton for the help! that worked great! i will definitely need to remember this stuff, thanks! Dec 9 '06 #6

 Expert 5K+ P: 6,596 Oh my god, thanks a ton for the help! that worked great! i will definitely need to remember this stuff, thanks! That's what this site is all about! You are welcome. Please keep posting, Barton Dec 9 '06 #7

 Expert Mod 2.5K+ P: 2,851 Maybe you know this but the Python list object has an in place sort method: Expand|Select|Wrap|Line Numbers a=[93,84,200,513,46,1,45,334,92,96,156,82,92,46,] a.sort() print a >>> [1, 45, 46, 46, 82, 84, 92, 92, 93, 96, 156, 200, 334, 513]   Dec 9 '06 #8

 Expert 5K+ P: 6,596 Maybe you know this but the Python list object has an in place sort method: Expand|Select|Wrap|Line Numbers a=[93,84,200,513,46,1,45,334,92,96,156,82,92,46,] a.sort() print a >>> [1, 45, 46, 46, 82, 84, 92, 92, 93, 96, 156, 200, 334, 513]   Yeah, I was going to tell him about that. Writing a sort routine is great exercise, though. Dec 9 '06 #9