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: - 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[0],i[1]) < 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 !!
5 3898
This line
is giving you an error, right? If your code is giving you an error, please post it in your question. Anyway, your error is - 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:
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.
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 -
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[0],i[1]) < 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
-
You wrote - x , y = randint(min_x,min_y),randint(max_x,max_y)
Maybe you meant - 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.)
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 -
def total_distances(cities):
-
distance = 0
-
for i in cities():
-
distance = distance + get_distance(i[0],i[1],i[0],i[1])
-
return i
-
I had the same problem and stumbled on this post, This is what I came up with to fix the issue: -
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[0] in range(x-30,x+30) and city[1] 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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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!!!!
|
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: 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...
|
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,...
|
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...
|
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...
|
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,...
| |