440,035 Members | 1,995 Online 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 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 repeat n times              generate random coordinates for new_city              too_close = True              while too_close == True                           too_close = False                           for each city in the city list so far                                       if the new_city < distance from city                                too_close = True                                generate new random city point                                break               add new_city to the results list   and here's what I have   def generate_cities(min_x,max_x,min_y,max_y,n,distance):     cities = [ ]     for x in range(n):         x , y = randint(min_x,min_y),randint(max_x,max_y)         too_close = True     while too_close ==True:         too_close = False         for i in cities:             if get_distance(x,y,i,i) < distance:                 too_close = True                 x , y = randint(min_x,min_y),randint(max_x,max_y)             else:                 too_close = False                 break     cities.append(x,y) Thanks for helping !! Nov 16 '08 #1
5 Replies

 Expert 100+ P: 469 This line Expand|Select|Wrap|Line Numbers 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 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 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 "", line 1, in 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 def generate_cities(min_x,max_x,min_y,max_y,n,distance):     cities = []     for x in range(n):         x , y = randint(min_x,min_y),randint(max_x,max_y)         too_close = True     while too_close ==True:         too_close = False         for i in cities:             if get_distance(x,y,i,i) < distance:                 too_close = True                 x , y = randint(min_x,min_y),randint(max_x,max_y)             else:                 too_close = False                 break     cities.append([x,y])     return cities   Nov 16 '08 #3

 Expert 100+ P: 469 You wrote Expand|Select|Wrap|Line Numbers x , y = randint(min_x,min_y),randint(max_x,max_y) Maybe you meant Expand|Select|Wrap|Line Numbers 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 def total_distances(cities):     distance = 0     for i in cities():         distance = distance + get_distance(i,i,i,i)     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 def make_cities(self):         cities = []         for i in range(0,n):             too_close = True             while too_close ==True:                 x , y = random.randint(10,990),random.randint(10,490)                 too_close = False                 for city in cities:                     if city in range(x-30,x+30) and city in range(y-30,y+30):                         too_close = True             cities.append([x,y])         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 