473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Syntax error problem: please help

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
8 1738
noob92
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
6,596 Expert 4TB
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
noob92
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
6,596 Expert 4TB
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
noob92
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
6,596 Expert 4TB
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
2,851 Expert Mod 2GB
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
6,596 Expert 4TB
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

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

Similar topics

14
by: sam | last post by:
When I run this SQL query: SELECT u.*, o.* FROM users u, orders o WHERE TO_DAYS(o.order_date) BETWEEN TO_DAYS('2003-09-20')-10 AND TO_DAYS('2003-09-20')+10
1
by: Steve | last post by:
I just spent waaaaaaaaaaaayy too much time trying to track down an error that was incorrectly reported just now, and I would like to see if someone can explain to me why it was reported that way. ...
29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
5
by: NanQuan | last post by:
I'm hoping someone can help me solve this error since I am at a total loss here. Usually I don't bother posting on any forums or groups on the internet and prefer to solve stuff myself but this is...
2
by: Phil Powell | last post by:
I am not sure why this is producing a SQL Server related error, but w/o having an instance of SQL Server on my machine to verify anything further, can you all help me with this? <!--- validate()...
3
by: KevLow | last post by:
Hi, Hope some kind soul can help me out here.. I'm trying to programmatically modify the column headings of a crosstab query such that it can be dynamic based on user specified period (Month...
20
by: W Karas | last post by:
Would the fear factor for concepts be slightly reduced if, instead of: concept C<typename T> { typename T::S; int T::mem(); int nonmem(); };
7
by: bryant | last post by:
Hi all. I am new to ASP and working in Expression Web. The following query displays the information I need in the gridview for a single record. SELECT "OE_HDR"."ORD_NO", "OE_HDR"."CUST_NAM",...
16
by: Chuck | last post by:
Please help me correct the statements in sub "BoundData" The following sub is in a module. Everything runs with no error messages, however,data is not reaching the text box. Data is being...
17
by: trose178 | last post by:
Good day all, I am working on a multi-select list box for a standard question checklist database and I am running into a syntax error in the code that I cannot seem to correct. I will also note...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.