0

Doing an assignment for uni and I am baffled. I know it must be something simple but for the life of me I cannot get the function mapLevel2 to use the result of getDistanceBetween in an addText.

I know the code would be better in a loop and that is the next step after this, but I need to get this code working first.

def mapLevel2():

  map=makePicture("C:/Users/Shaun/Documents/CPT120/Assignment 2/map.png")

  cityXvalue= [ 45,95,182,207,256,312,328,350,374,400 ]
  cityYvalue= [ 310,147,84,201,337,375,434,348,335,265 ]

  writePictureTo(map,"C:/Users/Shaun/Documents/CPT120/Assignment 2/marked-map.png")

  show (map)

  stops=requestInteger ("How many places would you like to visit?")

  if stops==2:

    start=requestInteger ("Where would you like to start your trip?")
    second= requestInteger ("What is the number of the next place you would like to visit")

  addLine(map,cityXvalue[start],cityYvalue[start],cityXvalue[second],cityYvalue[second])


  KMs=getDistanceBetween(cityXvalue[start],cityYvalue[start],cityXvalue[second],cityYvalue[second])

  addText(map,21,34,KMs)

  writePictureTo(map,"C:/Users/Shaun/Documents/CPT120/Assignment 2/marked-map.png")

  repaint (map)

def getDistanceBetween(x1,y1,x2,y2):

  dist=sqrt(pow(x1-x2,2)+pow(y1-y2,2))

  KMs=dist*10
  return KMs

Thanks for any help you can give me.

Antwane
  • 20,760
  • 7
  • 51
  • 84
  • @Martin Henriksen Apologies, this is the first time I have posted. I have realised some of the issues I had and now have the problem where in 'addText' the program is giving me an error of "The error was: 4th arg can't be coerced to String" – Shaun Nicholls Nov 16 '15 at 10:24
  • @ShaunNicholls, next time add error messages right into the question (not in comments). – Ilya Nov 16 '15 at 10:44

1 Answers1

1

If you need to put the result of the function getDistanceBetween as an argument of the function addText, just do it:

1) First of all, you need to implement the function getDistanceBetween:

def getDistanceBetween(x1,y1,x2,y2):
  return 10*sqrt(pow(x1-x2,2)+pow(y1-y2,2))

2) And after it you can use it. For instance, you can save the result:

KMs = getDistanceBetween(cityXvalue[start],cityYvalue[start],cityXvalue[second],cityYvalue[second])

3) Now you can use this variable KMs (also it is possible to do the same without intermediate variable):

addText(map,21,34,KMs)

4) Ok, now you have an error "The error was: 4th arg can't be coerced to String". It means, that you have to convert floating point value into string (you can read here how to do it):

addText(map,21,34,"{:.5f}".format(KMs))

Or without intermediate variable:

addText(map,21,34,"{:.5f}".format(getDistanceBetween(cityXvalue[start],cityYvalue[start],cityXvalue[second],cityYvalue[second])))
Community
  • 1
  • 1
Ilya
  • 4,583
  • 4
  • 26
  • 51
  • I tried changing the name to the variable _d_ "but still get this error The error was: 4th arg can't be coerced to String Inappropriate argument type. An attempt was made to call a function with a parameter of an invalid type. This means that you did something such as trying to pass a string to a method that is expecting an integer." – Shaun Nicholls Nov 16 '15 at 10:30
  • Changed the code to `addText(map,21,34,str(KMs))` and it works now. Thank you for your help – Shaun Nicholls Nov 16 '15 at 10:50