By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,900 Members | 1,078 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,900 IT Pros & Developers. It's quick & easy.

Syntax error problem: please help

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
  1. import random
  2. def sort_array2_4():
  3.     unsorted_array = random.sample(xrange(1000), 10)
  4. a=[93,84,200,513,46,1,45,334,92,96,156,82,92,46,]
  5. def bubblesort(a):
  6.     for swap in range(len(a)-1,0,-1):
  7.          for index in range(swap)
  8.              if a[index] > a[index + 1]:
  9.                a[index], a[index + 1] = a[index + 1], a[index]
  10.     return a
  11. while(more==True):
  12.     num=str(input("Enter a number to search for in list.\n")
  13.     if (num in bubblesort(a)):
  14.         print num + "is in list at number: ", bubblesort(a).index(num)
  15.     else:
  16.         print num + "is not recognized in list"
Dec 9 '06 #1
Share this Question
Share on Google+
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

bartonc
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

bartonc
Expert 5K+
P: 6,596
Great job! you were very close. next time include error message, ok?


Expand|Select|Wrap|Line Numbers
  1. import random
  2. def sort_array2_4():
  3.     unsorted_array = random.sample(xrange(1000), 10)
  4. a=[93,84,200,513,46,1,45,334,92,96,156,82,92,46,]
  5. def bubblesort(a):
  6.     for swap in range(len(a)-1,0,-1):
  7.          for index in range(swap):
  8.              if a[index] > a[index + 1]:
  9.                a[index], a[index + 1] = a[index + 1], a[index]
  10.     return a
  11. more = True  # must assign before reference
  12. while(more==True):
  13.     num=input("Enter a number to search for in list.\n")
  14.     # one too few parens caused syntax error no next line
  15.     # need the int anyway
  16.     b = bubblesort(a)  # use lots of variables for debugging
  17.     print b   # debug "trap"
  18.     if (num in b):
  19.         # print will convert to printable, so you don't have to
  20.         print num, "is in list at number: ", a.index(num)
  21.     else:
  22.         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
  1. import random
  2. def sort_array2_4():
  3.     unsorted_array = random.sample(xrange(1000), 10)
  4. a=[93,84,200,513,46,1,45,334,92,96,156,82,92,46,]
  5. def bubblesort(a):
  6.     for swap in range(len(a)-1,0,-1):
  7.          for index in range(swap):
  8.              if a[index] > a[index + 1]:
  9.                a[index], a[index + 1] = a[index + 1], a[index]
  10.     return a
  11. more = True  # must assign before reference
  12. while(more==True):
  13.     num=input("Enter a number to search for in list.\n")
  14.     # one too few parens caused syntax error no next line
  15.     # need the int anyway
  16.     b = bubblesort(a)  # use lots of variables for debugging
  17.     print b   # debug "trap"
  18.     if (num in b):
  19.         # print will convert to printable, so you don't have to
  20.         print num, "is in list at number: ", a.index(num)
  21.     else:
  22.         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

bartonc
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

bvdet
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
  1. a=[93,84,200,513,46,1,45,334,92,96,156,82,92,46,]
  2. a.sort()
  3. print a
  4. >>> [1, 45, 46, 46, 82, 84, 92, 92, 93, 96, 156, 200, 334, 513]
  5.  
Dec 9 '06 #8

bartonc
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
  1. a=[93,84,200,513,46,1,45,334,92,96,156,82,92,46,]
  2. a.sort()
  3. print a
  4. >>> [1, 45, 46, 46, 82, 84, 92, 92, 93, 96, 156, 200, 334, 513]
  5.  
Yeah, I was going to tell him about that. Writing a sort routine is great exercise, though.
Dec 9 '06 #9

Post your reply

Sign in to post your reply or Sign up for a free account.