user2598164
user2598164

Reputation: 35

Passing values through while loop error

I'm having trouble passing values through my while loop. Ive coded below with some pseudocode in Place still I'm unsure how to achieve the result but I have attached my code below to help if it can. The error portion where is passing the values through the while loop

Firstly my double list of values is as below. Which refers to name, easting, northing

Stationlist = [['perth.csv','476050','7709929'],['sydney.csv','473791','7707713'],['melbourne.csv','46576','7691097']]

And here is the code that I am using:

Import math 
global Eastingbase
global Northingbase
Eastingbase=476050
Northingbase= 7709929

Def calculateDistance (northingOne, eastingOne, northingTwo, eastingTwo):
    Base =100000
    deltaEasting = eastingTwo -eastingOne
    deltaNorthing = northingTwo -northingOne

    Distance = (deltaEasting**2 + deltaNorthing**2) **0.5
    If Distance < Base: 
     Return Distance

Def Radius():
1000

L=0
while L <= Len(Stationlist): 
    if calculateDistance(Eastingbase, Northingbase, Stationlist(row L, column 2),Stationlist(row L, column 3)) < Radius: 
        Relevantfilename = StationList (row L, column 1)
        print Relevantfilename
        L = +1

My error is that I am unsure how to pass the values from station list into the while loop and then continue the loop. I have tried using double list comprehension I.e [0][1] to pass the name but it won't work. Also adding plus 1 to L doesn't seem to continue the loop. Is there a way to pass all of the values from one row into the while loop and test it.?? I.e pass Perth.csv to Stationlist (row L, column 1), 476050 to Stationlist (row L, column 2) and 7709929 to Stationlist (row L, column 3)

Once that is done then repeat for melbourne and Sydney data

Upvotes: 2

Views: 116

Answers (2)

TerryA
TerryA

Reputation: 60014

There are many errors/misinterpretations in your code:

  • You should only use upper case names for classes. Infact, it won't even work for stuff like Import and If (as they are statements, and need to be spelt correctly :p)

  • To access elements in a list, you use indexing (not list comprehension, as you interpreted (that's actually a completely different thing)). For example, print stationlist[0][2] accesses the first item in the list, then the third item in the sublist (remember that indexing starts at 0)

  • If you want to add one to a number, you do L += 1 (note the order of symbols). This is the same as L = L + 1

  • I think you're misunderstanding functions (especially your radius one). All you need to do is radius = 1000. No functions needed :).

  • Some other Syntax/Indentation errors.

A while loop shouldn't be used here. a for-loop is better:

for station in stationlist: # Notice the lowercase
    if calculateDistance(eastingbase, northingbase, station[1], station[2]) < radius:
        print station[0]

Notice how I use Python's Indexing to get elements from a list. We don't need to include the row, because we are using a for-loop, which goes through each element in the list.

Upvotes: 2

zhangyangyu
zhangyangyu

Reputation: 8620

You should use L += 1 to increment the index. But it is not recommended in Python. And also Radius = 1000 no need to define a function.

for each in Stationlist:    
    if calculateDistance(Eastingbase, Northingbase, each[1], each[2]) < Radius: 
        Relevantfilename = each[0]
        print Relevantfilename

And I don't why the key words in your script begin with a capital letter. Python is case sensitive so it is wrong. And the globals are not needed.

Upvotes: 0

Related Questions