Sep-28-2019, 01:24 PM
Hello, I have an assignment.
And the output is:
1) Is there any way that I can store data or output that is inside the for loop, and have access to such data outside of the loop?
2) What could I change to have the line "Puppy 2 too far away" being printed after all position of puppies are being inputted?
Thanks for the help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
qxqy = input ( "Position of Queenie: " ).split() qx = int (qxqy[ 0 ]) qy = int (qxqy[ 1 ]) n = input ( "Number of puppies: " ) n = int (n) countn = 1 for countn in range ( 1 ,n + 1 ): countn = str (countn) pxpy_countn = input ( "Position of Puppy " + countn + ": " ).split() px_countn = int (pxpy_countn[ 0 ]) py_countn = int (pxpy_countn[ 1 ]) if abs (px_countn - qx) + abs (py_countn - qy)> 10 : print ( "Puppy " + countn, "too far away" ) if abs (px_countn - qx) + abs (py_countn - qy)> 10 : print ( "Puppy " + countn, "too far away" ) |
Output: Position of Queenie: 2 1
Number of puppies: 4
Position of Puppy 1: 10 1
Position of Puppy 2: 14 -2
- Puppy 2 too far away (In my code, this line is here)
Position of Puppy 3: 1 3
Position of Puppy 4: 0 4
+ Puppy 2 too far away (but it should be here)
I have a few questions: 1) Is there any way that I can store data or output that is inside the for loop, and have access to such data outside of the loop?
2) What could I change to have the line "Puppy 2 too far away" being printed after all position of puppies are being inputted?
Thanks for the help.