1. Rewrite the following script, replacing the FOR -loop with a WHILE -loop. Sav
ID: 3735481 • Letter: 1
Question
1. Rewrite the following script, replacing the FOR -loop with a WHILE -loop. Save your file as whileLoopino.py for n in range(-50,150,5): print n Note: The comma at the end of the print statement in NOT a typo. It’s a way to make consecutive print statements appear on the same line separated by a space.
2. Rewrite the following script, replacing the WHILE -loop with a FOR -loop and use the built-in range function. Save your file as forLoopino.py index = 9 while index <= 99: print index index = index + 10
3. Write a script (loopDistance.py) to find a set of distance tables from fire stations to schools in the feature classes in ‘C:/gispy/data/county.gdb’ (Download from Bb lab 7 data). Use the Point Distance (Analysis) tool to determine the distance from the fire stations (input point features) to schools (near features) and use a WHILE -loop to run the tool with ten different search radius values: 500 meters, 1000 meters, … , 5000 meters. Use ‘C:/gispy/scratch’ as the output directory for the ten output tables, instead of creating them inside the file geodatabase. Name the output files using the numerical distance as ‘dist500.dbf’, ‘dist1000.dbf’, ‘dist1500.dbf’, and so forth. Hard-code the input features, near features, and output directory in this script, so that no script arguments are needed.
4. To investigate the results of using the Simplify Line (Cartography) tool with various tolerance values, write a script (loopSimplify.py) that simplifies lines in ‘parkLines.shp’ with four different tolerance values. Use the built-in range function and a FOR -loop to perform line simplification for tolerance values: 30 feet, 60 feet, 90 feet, and 120 feet. Use the ‘POINT_REMOVE’ algorithm. Name output as ‘simp30.shp’, ‘simp60.shp’, ‘simp90.shp’, and ‘simp120.shp’.
Explanation / Answer
1. We can replace the for loop with while loop as shown below
Note: Please look for the comments
For Loop
for n in range(-50,150,5):
print n
While Loop
n=0
while(n<150):
print n //Printing n
n+=5 //Adding the step size
2. Converting while to for Loop
while loop
index=9 //initial value
while index <= 99:
print index
index = index + 10 //step size
Equivalent For Loop
for n in range(9,99,10):
print n
3. Question is not clear
4, Question is not clear.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.