d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Elementary Programming Question > Jython
12Next
Add Reply New Topic New Poll
Member
Posts: 5,299
Joined: Jul 4 2009
Gold: 2,632.57
Oct 28 2013 08:19pm
For this assignment, we have to create an animation from a set of pictures (like a flip book).
I have it working okay, but in my loop, it shows each image in a new window, making 5-15 windows, however many frames I run.

For now, this is my only problem. However, later, we will have to incorporate an option for looping for a certain time and idk if I can figure that out yet.


Anyway, here is my loop for showing the picture:

count=0
while count<endingPicture:
show(makePicture(getMediaPath(frameList[count])))
count=count+1

I tried a few things, mainly a show then repaint inside the loop, but realized it was just overwriting the previous "pic" so wasn't really repainting....

count=0
pic=makePicture(getMediaPath(frameList[count]))
show(pic)
while count<endingPicture:
pic=makePicture(getMediaPath(frameList[count]))
repaint(pic)
count=count+1

Anyway, thanks for any help. I'd really appreciate it. (This is my first programming course =/
Member
Posts: 28,331
Joined: Jun 9 2007
Gold: 11,700.00
Oct 28 2013 08:41pm
for a python question my suggestion is: holler for 'Azrad'
Member
Posts: 6,586
Joined: Jun 26 2011
Gold: 5,676.00
Oct 28 2013 08:49pm
I'd use a for loop myself, but I'm not so sure on this situation here.
Member
Posts: 5,299
Joined: Jul 4 2009
Gold: 2,632.57
Oct 28 2013 09:09pm
Quote (Dapwnzor @ Oct 28 2013 09:49pm)
I'd use a for loop myself, but I'm not so sure on this situation here.


Well, I don't really like for loops. They never seem to work for me.
If you need me to post more of the code, I can. I just figured it was a fairly simple problem, although I guess more information may be needed.

There's no hurry, it's not due until the eighth.

Quote (brmv @ Oct 28 2013 09:41pm)
for a python question my suggestion is: holler for 'Azrad'


I'll pm him, thanks.
Member
Posts: 6,586
Joined: Jun 26 2011
Gold: 5,676.00
Oct 28 2013 09:31pm
Quote (furbyjs @ Oct 28 2013 09:09pm)
Well, I don't really like for loops. They never seem to work for me.
If you need me to post more of the code, I can. I just figured it was a fairly simple problem, although I guess more information may be needed.

There's no hurry, it's not due until the eighth.



I'll pm him, thanks.


I haven't worked with things like this, but while loops are generally used for true/false situations.
for frame in framelist:
show(makePicture(getMediaPath(frame)))


Might work, not sure.
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Oct 28 2013 09:42pm
what follows is what Dapwnzor was discussing:


not quite sure what you are trying to do with drawing and such, but as far as looping goes, the simplest way is to do it like this:
Code
for frame in framelist:
do_something(frame)



For example if your framelist = ["dog", "cat", "mouse"], this will call the do_something function once as do_something("dog"), then do_something("cat"), then do_something("mouse"). It actually does not matter what the framelist is composed of... in fact here is another example:



Code
wootybooty = "I like turtles"
for letter in wootybooty:
print letter

This prints the string "I like turtles", one letter at a time.

A more confusing example:

Code
List_of_lists=[[1,2,3],[4,5,6],[7,8,9]]
for list in list_of_lists:
print list

This time, on each loop you will print out a whole list, on the first loop it will print [1,2,3], and on the second it will print [4,5,6], and on the 3rd it will print [7,8,9]


however, this one will fail with an error:
Code
my_fav_number = 655321
for number in my_fav_number:
print number

Python does not understand that you want it to look at the number digit by digit. I assume this is because it does not actually use numbers in base 10, so the "breaking point" between the digits would be different in binary than they are in base 10; so there is no obvious way to break them up like there was for lists and strings and such. If you really must do this here is how you can do it:
Code
my_fav_number = 655321
temp_string_fav_number = str(my_fav_number)
for number in temp_string_fav_number:
print int(number)

With a dirty trick we converted to string, spit it, then converted back.


another userful example might be this:

Code
for i, frame in enumerate(framelist):
do_something(frame)
This one is a little harder to explain, but it works the same as the first one, except you also have access to the index number of the current frame (from framelist) as "i" on each loop. This one is very useful.

This post was edited by Azrad on Oct 28 2013 10:03pm
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Oct 28 2013 10:08pm
Quote
count=0
pic=makePicture(getMediaPath(frameList[count]))
show(pic)
while count<endingPicture:
pic=makePicture(getMediaPath(frameList[count]))
repaint(pic)
count=count+1



how about this:
Code
show(makePicture(getMediaPath(frameList[0])))
for i, frame in enumerate(frameList):
if i ==0:
pass
repaint(makePicture(getMediaPath(frame)))

this causes it to skip the first (well 0th) frame, since you already drew it

This post was edited by Azrad on Oct 28 2013 10:09pm
Member
Posts: 5,299
Joined: Jul 4 2009
Gold: 2,632.57
Oct 28 2013 10:08pm
Quote (Azrad @ Oct 28 2013 10:42pm)
what follows is what Dapwnzor was discussing:


not quite sure what you are trying to do with drawing and such, but as far as looping goes, the simplest way is to do it like this:
Code
for frame in framelist:
    do_something(frame)



For example if your framelist = ["dog", "cat", "mouse"], this will call the do_something function once as do_something("dog"), then do_something("cat"), then do_something("mouse"). It actually does not matter what the framelist is composed of... in fact here is another example:



Code
wootybooty = "I like turtles"
for letter in wootybooty:
    print letter

This prints the string "I like turtles", one letter at a time.

A more confusing example:

Code
List_of_lists=[[1,2,3],[4,5,6],[7,8,9]]
for list in list_of_lists:
    print list

This time, on each loop you will print out a whole list, on the first loop it will print [1,2,3], and on the second it will print [4,5,6], and on the 3rd it will print [7,8,9]


however, this one will fail with an error:
Code
my_fav_number = 655321
for number in my_fav_number:
    print number

Python does not understand that you want it to look at the number digit by digit. I assume this is because it does not actually use numbers in base 10, so the "breaking point" between the digits would be different in binary than they are in base 10; so there is no obvious way to break them up like there was for lists and strings and such. If you really must do this here is how you can do it:
Code
my_fav_number = 655321
temp_string_fav_number = str(my_fav_number)
for number in temp_string_fav_number:
    print int(number)

With a dirty trick we converted to string, spit it, then converted back.


another userful example might be this:

Code
for i, frame in enumerate(framelist):
    do_something(frame)
This one is a little harder to explain, but it works the same as the one above, except you also have access to the index number of the current frame (from framelist) as "i" on each loop. This one is very useful.


They syntax is a little different, I guess because you're referring to python and I'm using "Jython" which I think is a little simplified.

Anyway, I have tried a for loop, but couldn't get it to work.

for frame in frameList:
show(makePicture(getMediaPath(frameList[frame])))

Returns an error
Code
The error was:sequence subscript must be integer or slice
Inappropriate argument type.
An attempt was made to call a function with a parameter of an invalid type. This means that you did something such as trying to pass a string to a method that is expecting an integer.


Here's the list
Code
while count<endingPicture+1:
frameList.append("frame"+str(count)+"."+str(suffix))
count=count+1


Each item is in the format frame1.png, frame2.png, etc. up to 15.
Member
Posts: 5,299
Joined: Jul 4 2009
Gold: 2,632.57
Oct 28 2013 10:09pm
I'll just put the whole code up...

Code
def main():
# Create empty list, request media path, starting file, ending file, and file type.
frameList=[]
setMediaPath()
startingPicture=requestInteger("Enter the number of the first picture to use.")
endingPicture=requestInteger("Enter the number of the last picture to use.")
suffix=requestString("Enter the file type suffix (png or jpg)")
count=1

# Populate the list
while count<endingPicture+1:
frameList.append("frame"+str(count)+"."+str(suffix))
count=count+1

# count=0
# while count<endingPicture:
# show(makePicture(getMediaPath(frameList[count])))
# count=count+1

for frame in frameList:
show(makePicture(getMediaPath(frameList[frame])))


/e It's completely possible my professor didn't intend for this program to open only one window, so I will ask him next class.

This post was edited by furbyjs on Oct 28 2013 10:11pm
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Oct 28 2013 10:16pm
Code
def main():
frameList=[]
setMediaPath()
startingPicture=requestInteger("Enter the number of the first picture to use.")
endingPicture=requestInteger("Enter the number of the last picture to use.")
suffix=requestString("Enter the file type suffix (png or jpg)")

for x in range(startingPicture, endingPicture+1):
frameList.append("frame" + str(x) + "." + suffix)

for frame in frameList:
show(makePicture(getMediaPath(frame)))


one of the problems you had is str(suffix) but suffix is already a string right?

/e
another problem is frameList[frame].... that is bad... lets say framlist look like frameList=["frame1.jpg", "frame2.jpg", "frame3.jpg"]
if you tried:
print frameList["frame1.jpg"]
that is just nonsense
you would want to do
print frameList[0]
or do it like i did in the code.

This post was edited by Azrad on Oct 28 2013 10:21pm
Go Back To Homework Help Topic List
12Next
Add Reply New Topic New Poll