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

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

P: 6 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
  10. and here's what I have
  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
Share this Question
Share on Google+
5 Replies

Expert 100+
P: 469
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

P: 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>
File "C:\Users\User\Desktop\", line 37, in generate_cities
x , y = randint(min_x,min_y),randint(max_x,max_y)
File "C:\Python25\lib\", line 215, in randint
return self.randrange(a, b+1)
File "C:\Python25\lib\", 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
Nov 16 '08 #3

Expert 100+
P: 469
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

P: 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
Nov 18 '08 #5

P: 1
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
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

Post your reply

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