Sep-28-2019, 01:24 PM
Hello, I have an assignment.
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.
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")And the output is:
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.