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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jcl43
    New Member
    • Nov 2008
    • 6

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

    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:
    Code:
    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 !!
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    This line
    Code:
    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
    Code:
    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:
    Code:
    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.

    Comment

    • jcl43
      New Member
      • Nov 2008
      • 6

      #3
      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\project 2.py", line 37, in generate_cities
      x , y = randint(min_x,m in_y),randint(m ax_x,max_y)
      File "C:\Python25\li b\random.py", line 215, in randint
      return self.randrange( a, b+1)
      File "C:\Python25\li b\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
      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

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        You wrote
        Code:
        x , y = randint(min_x,min_y),randint(max_x,max_y)
        Maybe you meant
        Code:
        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.)

        Comment

        • jcl43
          New Member
          • Nov 2008
          • 6

          #5
          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
          Code:
          def total_distances(cities):
              distance = 0
              for i in cities():
                  distance = distance + get_distance(i[0],i[1],i[0],i[1])
              return i

          Comment

          • pml0904
            New Member
            • Jun 2010
            • 1

            #6
            I had the same problem and stumbled on this post, This is what I came up with to fix the issue:
            Code:
            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

            Comment

            Working...