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"
8 1738
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.
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.
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?
Great job! you were very close. next time include error message, ok? - 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"
Great job! you were very close. next time include error message, ok? - 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!
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
bvdet 2,851
Expert Mod 2GB
Maybe you know this but the Python list object has an in place sort method: - 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]
-
Maybe you know this but the Python list object has an in place sort method: - 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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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.
...
|
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"...
|
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...
|
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()...
|
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...
|
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();
};
|
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",...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
| |