473,405 Members | 2,160 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,405 software developers and data experts.

Beginner Python-er !!!Need help urgently !!!

6
Ok...so I have this psedecode that I want to follow but it keeps giving me an error so I'm not sure where I went wrong.

Here is the psedecode:
Expand|Select|Wrap|Line Numbers
  1. repeat n times    
  2.          generate random coordinates for new_city    
  3.          too_close = True    
  4.          while too_close == True        
  5.                   too_close = False        
  6.                   for each city in the city list so far            
  7.                           if the new_city < distance from city                                too_close = True                                generate new random city point                                break     
  8.          add new_city to the results list
  9.  
  10. and here's what I have
  11.  
  12. def generate_cities(min_x,max_x,min_y,max_y,n,distance):
  13.     cities = [ ]
  14.     for x in range(n):
  15.         x , y = randint(min_x,min_y),randint(max_x,max_y)
  16.         too_close = True
  17.     while too_close ==True:
  18.         too_close = False
  19.         for i in cities:
  20.             if get_distance(x,y,i[0],i[1]) < distance:
  21.                 too_close = True
  22.                 x , y = randint(min_x,min_y),randint(max_x,max_y)
  23.             else:
  24.                 too_close = False
  25.                 break
  26.     cities.append(x,y)
Thanks for helping !!
Nov 16 '08 #1
5 3898
boxfish
469 Expert 256MB
This line
Expand|Select|Wrap|Line Numbers
  1. cities.append(x,y)
is giving you an error, right? If your code is giving you an error, please post it in your question. Anyway, your error is
Expand|Select|Wrap|Line Numbers
  1. TypeError: append() takes exactly one argument (2 given)
Which means that you have called append with too many arguments. You can only append one thing to the list at a time. What you should probably do is append a single list that contains x and y:
Expand|Select|Wrap|Line Numbers
  1. cities.append([x,y])
There are two other thing wrong with your code that are not causing errors, but you should fix them anyway. You have a problem with your indentation so that the while loop and the call to append are not inside the for loop like they should be. And you are using x as the counter variable in your for loop as well as for the x-coordinate, which is a very bad thing. Rename the counter variable.
By the way, it would be helpful if you used code tags around your code. Put [CODE] before the code and [/CODE] after it, so it shows up in a code box and the indentation isn't wrecked. Thanks.
Hope this helps.
Nov 16 '08 #2
jcl43
6
This function is suppose to generate a list of cities with random cooridnates but also making sure that they are not too close to each other
but now when i run it, it doesn't give me an error(only if i use generate_cities(-10,10,-10,10,10,10) but if i switch the numbers say to -30 or something it gives me this error :

>>> generate_cities(-10,30,-10,10,10,10)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
generate_cities(-10,30,-10,10,10,10)
File "C:\Users\User\Desktop\project2.py", line 37, in generate_cities
x , y = randint(min_x,min_y),randint(max_x,max_y)
File "C:\Python25\lib\random.py", line 215, in randint
return self.randrange(a, b+1)
File "C:\Python25\lib\random.py", line 191, in randrange
raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart, istop, width)
ValueError: empty range for randrange() (30,11, -19)

This one's my code
Expand|Select|Wrap|Line Numbers
  1. def generate_cities(min_x,max_x,min_y,max_y,n,distance):
  2.     cities = []
  3.     for x in range(n):
  4.         x , y = randint(min_x,min_y),randint(max_x,max_y)
  5.         too_close = True
  6.     while too_close ==True:
  7.         too_close = False
  8.         for i in cities:
  9.             if get_distance(x,y,i[0],i[1]) < distance:
  10.                 too_close = True
  11.                 x , y = randint(min_x,min_y),randint(max_x,max_y)
  12.             else:
  13.                 too_close = False
  14.                 break
  15.     cities.append([x,y])
  16.     return cities
  17.  
Nov 16 '08 #3
boxfish
469 Expert 256MB
You wrote
Expand|Select|Wrap|Line Numbers
  1. x , y = randint(min_x,min_y),randint(max_x,max_y)
Maybe you meant
Expand|Select|Wrap|Line Numbers
  1. x , y = randint(min_x,max_x),randint(min_y,max_y)
You still have to indent the while loop and the append and rename the counter variable, otherwise your code will not work correctly.
(Actually, there doesn't seem to be a problem with the counter variable, but I think I will sleep better if you give it a different name. It's very important.)
Nov 17 '08 #4
jcl43
6
so after that part, I have to make a function that returns a list of total distances between each of the cities in the cities list (a list of pair-lists) and I have to have it so the function calculates the total distances between each city and all the other cities. The index of the results list should correspond to the index in the cities list so that index i in the results list represents the total distance between cities[i] and all the other cities.

I understand that it's telling me what to do but don't understand how to write it out :(

right now I have
Expand|Select|Wrap|Line Numbers
  1. def total_distances(cities):
  2.     distance = 0
  3.     for i in cities():
  4.         distance = distance + get_distance(i[0],i[1],i[0],i[1])
  5.     return i
  6.  
Nov 18 '08 #5
I had the same problem and stumbled on this post, This is what I came up with to fix the issue:
Expand|Select|Wrap|Line Numbers
  1. def make_cities(self):
  2.         cities = []
  3.         for i in range(0,n):
  4.             too_close = True
  5.             while too_close ==True:
  6.                 x , y = random.randint(10,990),random.randint(10,490)
  7.                 too_close = False
  8.                 for city in cities:
  9.                     if city[0] in range(x-30,x+30) and city[1] in range(y-30,y+30):
  10.                         too_close = True
  11.             cities.append([x,y])
  12.         return cities
  13.  
Some notable things: The else statement causes the while loop to break before it checks if all of the cities. I have my max distances and number of cities made hard coded in, but I'm sure that you can change it to fit your needs
Jun 25 '10 #6

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

Similar topics

1
by: Zeeshan | last post by:
Hi Everybody, I want to search the filename recursively for "this is the code original" using linux command grep or ls or any other Result should be as follows: FilenamePath and Linenumber in...
1
by: Dhiraj | last post by:
hello, everybody, this is Dhiraj here. I need help urgently, as i am getting error on my web page. The error is "Microsoft JET Database Engine error '80004005'" whenever i want to open links of...
3
by: Josh | last post by:
I am writing a program where the user inputs currency in US dollars. I want the program to only accept valid currency input, converting the string into the proper type of variable (double?), and...
2
by: u1jd | last post by:
Hi, I am trying to do the following but i get an error, is there no way i can use eval to print have it assigned to a predeterminded variable. eval (object_name + "_CalCalendar") = new...
5
by: Shani | last post by:
I need help setting up a database which includes a list of about 1200 urls. Anyone who accesses the database should be able to input wheather the url is good or bad. I want to share the databse...
4
by: PINKA | last post by:
hey can any one help me in i want to make a program in "C" in which user can enter a row , column and matrix(2D ARRAY) based on that row and column. that matrix should be diplayed but the...
3
by: karizmatrix22 | last post by:
Hi. I need a application where accept user password. The java program should do the following: 1. Application accept user password from keyboard 2. It has to be Less than six character and not...
4
by: supermancc03 | last post by:
I have a List<Point> X(Point pos) which returns a list of coordinates of "neighboring cells" I also have a function getY(int x, int y) that takes in a set of points and returns me a double. I need...
2
by: chani | last post by:
I dont know where start my project in "files sharing" if someone can help me it's will be nice i'm so nervous from this... thanks a lot!!!!
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.