Python Forum
Storing data/output from a loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Storing data/output from a loop
#1
Hello, I have an assignment.
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.
Reply
#2
You typically store data from a loop using a list and append:

data = []
for x in range(5):
    data.append(x * x)
For the second question, I would use a list as shown above to store which puppies are too far a way. Then you can have a second loop after the input loop to print them.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
It's probably my fault that I forgot to mention there are other cases like this one:
Output:
Position of Queenie: 2 1 Number of puppies: 4 Position of Puppy 1: 15 15 Position of Puppy 2: 14 -2 Position of Puppy 3: 16 16 Position of Puppy 4: 0 4 Puppy 1 and Puppy 2 and Puppy 3 too far away
Do I solve it using the same method?
Thanks.
Also, the "Number of Puppies" may not necessary be 4, it is a number inputted by the user.
Here is the problem description: (I cannot upload photos)

Queenie is living in an infinite planar taxicab geometry world. She is living together with n puppies.

Queenie is worried to lose sight of her puppies. You will write a program that let Queenie know which of her puppies are too far away from Queenie. A puppy p at position (px, py) is too far away from queeny at (qx, qy) if
∣px−qx∣+∣py−qy∣>10
where |x| denotes the absolute value of x.

Let
> the position of Queenie,
> n, and
> the positions of the puppies
be provided by the user.

Anyway, thanks for the response.
Reply
#4
That would all work the way I was saying. Appending to a list is perfect when you don't know how many items you will have ahead of time. It just adds each new item onto the end of the list, and it can do that until you run out of RAM.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
It seems like we come from the same university.
Reply
#6
I have tried using the append function along with some other changes, and it outputs an error.
qxqy=input("Position of Queenie: ").split()
qx=int(qxqy[0])
qy=int(qxqy[1])
n=input("Number of puppies: ")
n=int(n)
list=[]
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:
        list=list.append(countn)
print("and".join(list))
The error is as follow:
Output:
Position of Queenie: 2 1 Number of puppies: 4 Position of Puppy 1: 10 1 Position of Puppy 2: 14 -2 Position of Puppy 3: 1 3 Position of Puppy 4: 0 4 Traceback (most recent call last): File "main.py", line 15, in <module> print("and".join(list)) TypeError: can only join an iterable
So I have more questions.
1) What does the error means? How could I fix the error?
2) I tried to print the list outside of the loop, and the output is "none". What's wrong with my list?
3) Could you elaborate a bit more about the list and append example on #2?
Thanks for your help.
Reply
#7
The problem is line 14. The append method modifies the list in place, so you don't need to assign the result to anything. In fact, the result is None. There error is because of that None. The join method needs a list or other sequence/iterable. None is none of those things, so you get an error.

Note that naming your list 'list' is a bad idea. The name 'list' is already used in Python, so if you use it for one of your variables, you can no longer access the built-in list function.

To fix it, rename your list 'strays', and remove the assignment so it's just:

strays.append(countn)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I experimented with a loop, and I don't understand how I got my output thinwheats 7 2,907 Mar-18-2020, 07:33 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020