Fall 2006 Homework 4 questions page
Homework 4 Questions Page:
It seems like the more we learn in this class, the less we actually use it... I realize that you guys are trying to provide a tie in but wouldnt it just be easier to write something like this without jes?
| This is how I think about it - When does it make sense to write code to do something and when does it not? If I am doing something once and only once, then it almost becomes a waste of time to write code. However if I am doing something over and over again, it is more than worth that time investment. For example, lets say I want to decrease the red in an image. I certainly wouldn't want to do that by hand pixel by pixel by pixel - Let the computer do all the monotonous stuff. However if it were something like I needed to know what my average was in one of my classes right now, I would probably do that by hand. It makes sense because I may take only one class where I have completed lets say 3 out of 5 assignments where they are weighted 15%, 10%, and 25% ,respectively, of my final grade. So, in respect to this assignment, webpages have the same basic tags, right? and you need to put 5 different images on the page....so instead of running the risk of making a mistake little mistake in your HTML, you can have JES autogenerate the HTML tags so all you have to change is what is displayed Toni Walden |
how do we resize images in html?
| Like this: <img width="50" height = "90" src="http://www.linksomwhere.com/thisisabogusfilename.jpg"> Colin Potts (To minimize confusion, I have removed a partially incorrect answer to this question. The plus signs in that answer that someone was asking about are Coweb-specific symbols, not regular HTML, and are explained if you go to an edit page. Everyone else, ignore this....) BTW, this page is now unlocked again. Please don't lock it when posting questions! |
do the picture files have to be url's or can we save the pic from the internet and then just type the name of the file we saved it as?
| The pictures should be variable inputs to your code. They will be whatever we choose as TAs. Amanda Bennett |
what is the maximum number of people you can collaborate with? do you take this number into account?
| As many as you need. Just make sure you list them all on your homework. And I'd strongly suggest making sure that you understand what is going on in the code, there's always a chance you will have to do something similar on Tests/Quizzes. Amanda Bennett |
file=open("jwaintraub3.html","wt")
def writeStory():
file.write("""
This
Is
A
Story""")
def makeMadLib(dir):
file.write("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transition//EN" "http://wwww.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Jeff's Madlib</title>
</head>""")
writeStory()
file.write("""
</body>
</html>""")
file.close()
How come when I use pickAFolder() as my directory input, it doesn't create an html file in that folder?
| You need to make sure that your directory is an input variable. The reason for this is so you can add the string of the directory plus the name of your file to allow the file to be written to the given directory. (that sounded really confusing when I typed it, but hopefully you understand). Amanda Bennett |
In the example below, what is the point of this part: ("C:/mediasources",
| This is defining the directory. It's what you'd put in if you had dir as an input variable. Amanda Bennett |
Also, will all of the text below will go in our function like this: def makeMadLib(all the text below)?
makeMadLib("C:/mediasources","http://coweb.cc.gatech.edu:8888/uploads/cs1315/4418/Mark.jpg","http://coweb.cc.gatech.edu:8888/uploads/cs1315/4418/Barbara.jpg","http://coweb.cc.gatech.edu:8888/uploads/cs1315/4418/horse.jpg","http://coweb.cc.gatech.edu:8888/uploads/cs1315/4418/beach.jpg","http://coweb.cc.gatech.edu:8888/uploads/cs1315/4418/bird.jpg")
| Nope. Each of these inputs should be variables, not hard coded. These were just examples of how the code will be used. Amanda Bennett |
I cannot figure out why this is not legal jython:
male='<image width=50 src="'+http:\\www.selvesandothers.org\IMG\auton2088.jpg+'">'
But this is:
person1='<image width=50 src="'+person1url+'">'
Any hints?
| Ok, so the best way to tell exactly what JES is reading as a string is to look at the color of the text (strings are a brownish color). It looks me as though JES is reading your second example correctly because you are using a predefined variable for the image source. In the first example, it is not legal Jython because JES has no clue what http://www.... is. It's the minute difference between using a string as input and using a variable as input that will often trip you up. I hope this helps, and feel free to let me know if I am unclear about anything :) Toni Walden |
so are the plus signs necessary or not?
| Depends on which one you are referring to.....Toni Walden |
| Yes, the plus signs are needed any time you are adding strings...or adding anything for that matter. Amanda Bennett |
I used to think this, too, but found out today that both of the following lines are valid Python:
myName = "Colin " + "Potts"
myName = "Colin " "Potts"
I don't like the second form, because if you have embedded single and double quotes it's pretty hard to work out what is going on without the plus. But it does work. Of course, if you have a string variable, then you really do need the plus sign:
firstName = "Colin "
myName = firstName + "Potts" #Valid
myName = firstName "Potts" #Invalid
my images come up as boxes with red X's in the middle. how do i fix this?
| Are you defining the correct path for the photos? Are you capitalizing them correctly? Amanda Bennett |
Does our html page have to be validated using W3?
| No, but make sure your HTML is correct with all opening and closing brackets and proper headings and whatnot. But the main goal is to get you to auto-generate the HTML, not learn if you can write HTML correctly. We already learned that with your last assignment. Amanda Bennett |
HW 4 states that your text should be at least three paragraphs and 20 sentences. Does this mean we are required to indent our paragraphs or leave a space between them? Or, can we just write out 20 sentences without any spacing or indention?
| Think about how to identify a paragraph in html... is there a tag for this exact purpose? Liz Helms |
I got a "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."
| Please do not post all of the code for the assignment to the Coweb. It would be better and easier to just go to a TA's office hours or e-mail a TA with your code to get help. Thanks! Brittany Duncan |
Any suggestions on how to split this assignment into two functions?
| You have to add 4 pictures right? This would be very similar code for each one... Just something to think about. Thanks! Brittany Duncan |
Are we supposed to submit the code on WebWork because Homework 4 is not in the drop down menu to select?
When the TA enters the parameters, will they enter the directory with TWO slashes (i.e. "C:\\temp")? This is the only way I can make my function recognize the directory.
| Either that, or they'll use one forward slash ("C:/temp"), or they'll use r (e.g., r"C:\temp"). Mark Guzdial |
When I input internet urls my code works perfectly, but when I use ".jpg" files from my computer like in Guzdial's example I only get text(not even the little boxes with red x's). I set the media path, so what gives?
| Are the .jpg files in the same directory as the HTML? Remember that just "blah.jpg" is only a valid URL if the file "blah.jpg" is in the same directory as the HTML file. Mark Guzdial |
how do you get your pictures to be different sizes?
| It's in the book and in the slides. < image height=X width=Y /> Mark Guzdial |
why does jes only acknowledge my string when it is on the same line, as soon as I press enter, the colors are switched and my images become strings and my strings become regular black text.
| If you want to have an empty line in your text, think about how you would do that in HTML. You can't just hit enter or JES will think that you are trying to enter the next line of code. Thanks! Brittany Duncan |
<img src="+ male +" />
Is this not correct? For some reason, I can't get the picture to appear.
| This looks good to me. Your variable male must have a value that doesn't correspond to a file name when you call your makeMadLib function. Are the other images appearing? Maybe you are misspelling the name, or the file is in a different folder. Colin Potts |
In order for our test pictures to show up on our webpage do we have to have them saved on our computer or is just the img url ok for this? The latter doesn't seem to be working for me.
| The TA could test your program with either, and it shouldn't make any difference. If non-local URLs aren't working for you, are you giving complete URLs (with the "http://" prefix?) If you just say "www.somesite.tv/picture.jpg", this will be interpreted as the file picture.jpg in the folder www.somesite.tv — which is unlikely to exist! You need to say "http://www.somesite.tv/picture.jpg" Colin Potts |
Can we make the three paragraphs in the same shape as the example? As in, 3 groups of 6-7 lines separated by spaces?
I can't seem to get my directory to work correctly. I have a mac and this is what my code looks like:
def makeMadLib(directory, male, female, pet, place, food):
setMediaPath(directory)
My input looks like this:
>>> makeMadLib("/Users/jilldavis/MediaSources-MainImages/", "knight-londontower.jpg", "barbara.jpg", "fish.jpg", "edinburgh-castle.jpg", "daisies.jpg")
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.
Any ideas as to what I'm doing wrong?
Jill Davis
| The significant part of the error message is the following line: "TypeError: setMediaPath() too many arguments; expected 0 got 1." setMediaPath doesn't take an argument; it gets its input through the user interface that pops up. Colin Potts |
Is it possible to create a function that has optional variables? For example, I was writing a function
def img(var, width):
and in the function I'm attempting to omit adding "width=###" to the image tag being generated if there is no width given.
For example, if width 100 was given, the function would generate <img src="1.jpg" width="100" />
but if no width was given, <img src="1.jpg" />
| Yes and no. It is possible to do this. (The function max, for example, takes any number of arguments.) However, the syntax is a headache, and we would rather not go into it in this course. Here is a workaround that you could use: whenever a special width value is specified, your program could ignore it. This could be a width of 0 or a width less than zero. (This technique is also a one reasonable way do do some unobtrusive error handling.) Colin Potts |
Hey, I just want to be sure that I'm clear on this, but the in terms of turning this homework in, is there a gallery where we need to post out html products? All we need to do is submit it to webwork, right?
| Yup, that's correct! All you have to do is submit your hw4.py file to webwork, and that's it! :) Sweta Vajjhala |
I have this code and JES tells me "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." Which ones do I have as integers?
def makeMadLib(male,female,pet,place,food):
file=open("mschott3.html","wt")
male=picture(male)
female=picture(female)
pet=picture(pet)
place=picture(place)
food=picture(food)
file.write("""<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transition//EN' 'http://wwww.w3.org/TR/html4/loose.dtd'><html><head><title>My Madlib</title></head>""")
file.write("<body>")
file.write("<h1> 'The Story of a Couple'</h1>")
file.write("""</body></html>""")
file.close()
def picture(name):
return'img src="'+name+'" height=50/>'
| "This means that you did something such as trying to pass a string to a method that is expecting an integer." doesn't mean that this is what you are actually doing, but you are doing something such as that. Another example of a similar error is trying to call a function with 6 arguments when it is expecting only 5. Is this maybe what you are doing? Are you giving makeMadLib a directory argument – which your function, as written, is not expecting? (Your function worked for me when I called it with five string arguments.) Colin Potts | |
does our second function have to be extremely difficult? I can't think of too many things I would need a second function for and that worries me a little...
| No, although you could make it extremely difficult if you really want :) There are two examples on this page that you could use as inspiration. Colin Potts |
my HTML page keeps showing up with all text (def madlib(blah, blah, blah) - everything). To be honest, I don't know why it's doing it and how to fix it. I didn't really expect it to react like that
| This is a bit like telling a car mechanic that your car is making funny noises. Not much to go on! Either be more specific or show your code and the source of the HTML that it generates to a TA. Colin Potts |
i loaded my function and typed in the input. the function didn't give me an error message, but nothing showed up. is my madlib suppose to show when i load it?
| See previous comment. Colin Potts |
| Well first, does anything ever show when you load a function? I'm guesing you're really talking about running the function. Your madlib won't 'pop up', but you should be able to find it in the directory that you included as an input. But, here are a couple of questions to answer to see if things are correct. 1. Did you include file.close()? 2. Did you make sure your directory was included in the open statement of the file? Check these out, they're typical causes of files not showing. Amanda Bennett |
def madlib(maleurl,femaleurl,peturl,placeurl,foodurl):
file=open("madlib.html","wt")
man='<image width=50 src="'+maleurl+'">'
woman='<image width=50 src="'+femaleurl+'">'
pet='<image width=50 src"'+peturl+'">'
place='<image width=100 src="'+placeurl+'">'
food='<image width=50 src="'+foodurl+'">'
This is the beginning of my code but when I load it and run the function everything is displayed. It shows
def madlib(maleurl, femaleurl, peturl, placeurl, foodurl): file=open...etc. It doesn't acutally run correctly.
| I don't quite get what you mean "everything is displayed". Displayed where? You might want to make sure that you are also taking in a directory as your first input. And then, you want to make sure you're writing the file in the directory. But beyond that, I just don't understand what you're asking. Amanda Bennett |
I mean, on my webpage instead of pictures and sentences showing up I get the entire function, quotes and all. I think my problem is the directory. I don't remember any of the examples taking in a directory so I must have missed it. So where I have def madlib(maleurl,femaleurl,peturl,placeurl,foodurl):
I should have def madlib(DIRECTORY,maleurl,femaleurl,peturl,placeurl,foodurl):
...maybe?
| I think that you are loading your Python source into the browser rather than the HTML file it is generating. Colin Potts |
I don't think I understand the difference. Even the codes taken directly from the book or the notes and saved as something.html - they all do the same thing. Everything looks like the code from the comments page posted by Mark a few days ago.
| I assume by this stage of the course that you do understand that HTML and Python are different languages and that the purpose of this homework is to write a Python program that will generate HTML. There is nothing wrong with using a web browser to view Python source files. They will display – kind of correctly. Your original question was why you are not seeing the HTML that your program is generating. The answer is that you are not looking at the right file: you are looking at the program, not the file that it outputs. Colin Potts |
Why my html works perfectly when using microsoft explorer but does not show the images when using mozilla firefox? I mean, the text is displayed correctly but instead of images I get only blank spaces.
| Bizarre. You are probably bending the rules of strict HTML in a way that IE allows but Firefox doesn't. If you validate the HTML, you may be able to pinpoint the problem. Don't worry about that, though. If your HTML displays correctly using one of the major browswers, that's good enough. Colin Potts |
Is the directory input only for the purpose of setting the media path there?
| Nope. You want to use it to directly output yout HTML file. Look a couple of questions below this for Colin's example on how to do it. Amanda Bennett |
what does, "Your html file displayes the required information: Number of images, sizes of images, length of story, etc." mean exactly?
| We're just saying to make sure that you've included enough pictures, enough paragraphs, etc. etc. Just double check your work. Amanda Bennett |
What does it mean in the HW checklist that the HTML should display all the required information like, number of images, sizes of images, etc.?
How exactly do we write a file to a particular directory again?
| Oh, alright then! Suppose your directory is named 'dname' and your filename is 'fname', you open/create the file as follows: file=open(dname+"//"+fname, "wt") and then use file.write() to write to that file. Colin Potts |
Okay so I thought we were suppose to call our function with one input like:
def makeMadLib(directory):
not
def makeMadLib(male,female,pet,place,food):
| No, no, no. Don't think; read! :) Here's what the assignment specification says: "Your function takes in one directory (as a string) and five input URLs". So the correct definition line should have directory in it as an argument and male, female, pet, place, and food. Colin Potts |
Thank you so much!!
def body():
bodystring = "<p>One day "+male+" meet "+female+" in a restaurant near the "+place+"."
return bodystring
why won't this work on addition to my code it keeps say to check the second line of the function above. It says that "A local or global name could not be found. You need to define the function or variable before you try to use it in any way." But I have already defined it in the function above it.
| The variables male, female and place are not defined in the function. You have to pass them in as parameters for the names to have values. Colin Potts. BTW, I put your code in <code> tags. The error wasn't obvious the way you originally posted it. |
Whats the difference, when writing the directory and writing the inputs? Isn't there no difference just that the directory goes first? Also I can not find the file that my function is suppose to write? Do I need to first create an html with gtg###.html and then do the function or is the function suppose to create the gtg####.html file and how do we create a html file?
| You sound really confused. Talk to a TA in person. Colin Potts |
The directory needs to come first because the TA will use that as the first input. Just use that input as part of the open() call, and it should generate the html file when you write to and close() it.
Why does the following line of code keep getting returned with an error? file.write(“+female+” replied, 'well then I guess you should study harder!')
I've tried using only single quotes for the actual quote, trying using double, and triple and none of them seem to work.
| (1) Quotes go around text, not variables. female is a variable. (2) The plus sign concatenates two strings, as in a+b and so needs to go between them. It looks like you think that +female+ is some kind of name. Colin Potts |
How can I make an indentation? I know the paragraph tag separates groups of text with a whitespace, but how do I indent?
i can't get my pic to be bigger. even when I type in the EXACT same thing as before (male='') except with a 200 (male='') I get a "> in it's place. I've tried FOREVER and know it's not quotes or anything dumb. Please help me
| The greater-than sign that you see in the browser is the result of the browser getting confused by the HTML you are generating. If you actually read the HTML (by viewing the source in the browser) you will see what is wrong with the tag, So the question is, why is the program generating that HTML? The problem is that you're saying male = "something"+male on one line and then using the variable male again on the next without realizing that you have changed its value. You're giving the same variable name both to the name of a picture file (male) and to the string that contains the HTML fragment referring to the picture. Colin Potts |
is this "+name+" the same as doing this """+name+""" in a paragraph?
| Yes, they are both quoted strings. But, you should reserve triple quotes for the outermost quotes for multi-line strings or strings that also contain single or double quotes. Also, I think you might be asking this question because of a misconception about fragments like "+name+" (and if you are not, I know that other students have this confusion). "+name+" should probably come from the middle of a line like this: string="First"+name+"Second" so that the first quote in "+name+" is the ending quote of the first string and the second quote is the starting quote of the last string. Neither quote belongs with the variable called name. Colin Potts |
I want to use urllib to pull the images before generating the HTML so I can constrain the proportions of the picture when scaling them.
The problem is, I can't get urllib to download the pictures in the format "makePicture()" requires. I understand how to embed the images from JES into HTML, this is just to make things look better (i.e.: like they weren't made in MS Word) in relation to the scaling of images.
import os
import urllib
def netTest(image):
connection = urllib.urlopen(image)
file = connection.read()
newPic = makePicture(file)
connection.close()
def do():
netTest("http://coweb.cc.gatech.edu:8888/uploads/cs1315/4418/Mark.jpg")
The error (truncated for sake of uncomprehensible ASCII):
makePicture(filename): There is no file at /Applications/JES.app/Contents/Resources/Java/ÿØÿà
Robby Thrash
Wow. I'm impressed by your attempt. However, I don't think that you are going to be able to do this without some object-oriented programming (....Mark?...)
makePicture isn't going to do it for you, since that takes a file name. It would be more reasonable to attempt the following (even though it doesn't work!) after reading the file:
show(file)
writePictureTo(file, somefilenameorother)
since the variable returned by connection.read() ('file', you named it) is the data coming from that URL. In the HTML examples that we did in class, it was text. In this case, it is binary data of the picture itself, not a text, and that is why you got a lot of weird ASCII characters when you tried to treat it as a string. The reason that my first stab at a solution doesn't work is that the data is not a valid JES picture. You may have to pass on this and live with pictures that don't meet your aesthetic standards! but let me dig in the documentation some more. Someone with knowledge of the inner workings of the media library may weigh in. Anyone? Colin Potts
| Ok, here is the answer. See if you can work this out without me giving you the code. You are opening the connection and reading from it. You need therefore to create a local jpg file and write that data straight out to it. Unlike the other files we have written to, it is a byte/binary file, not a text file, so you have to open it with the correct mode. Don't use writePictureTo, just use file.write. Bob's your uncle, as we say in the Old Country. Colin Potts |
| Thanks, that's makes sense (though I'm not too sure I want to be writing unnecessary files to TA's computers). Even though I realized that the img HTML tag will automatically constrain proportions when scaling if only one dimension is given... Robby Thrash |
Is there a way to have an open ended quote inside a string? For example, I want to show(including quotes): "Blah blah text picture blah blah more text."
I'd have to make a string that said """ "Blah blah text""" and then + picture + """ blah blah more text" """ but I can't end the string without closing the quote marks.
| I think you have the answer. Your triple quotes are enclosing the string. Although they have to match, they are not part of the string. The unmatched ("open-ended") double quotes are contained in the string, so they don't need to match if you don't want them to. If you print such a string, you will see the unmatched quotes inside. ("Inside" here could mean right at the beginning or end of the string.) Additionally, and this is clearly what you are wanting to do, you could concatenate two strings, one of which has the open quote at the beginning of it, and the other has the close quote at the end of it. Those two strings each have one, unmatched quote inside. The resulting string would have an open at the beginning and a close at the end, but the two strings that you splice together to make it would have only one unmatched quote each. (And of course, in the process you could splice other stuff, such as image tags, between them.) Colin Potts |
When I run my function, only my first pic is showing. I'm using seperate functions to add the pictures to the body. I feel like it has something to do with my return function, but when I remove it there is an error with calling an invalid function.
| View the source of the page. How do the subsequent ill-formed image tags differ from the correct first one? Answering that should help you work out what is wrong with the code that generates the HTML. Colin Potts |
def makeMadLib():
file=open("gtg773x.html","wt")
maleperson='<image width=100 src="'+"C:\work\mp.jpg"+'">'
femaleperson= '<image width=200 src="'+"C:\work\fp.jpg"+'">'
pet='<image width=50 src="'+"C:\work\dog.jpg"+'">'
place1='<image width=50 src="'+"C:\work\hell.jpg"+'">'
food='<image width=50 src="'+"C:\work\food.jpg"+'">'
file.write("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transition//EN" "http://wwww.w3.org/TR/html4/loose.dtd">
I have this. when i call it it jes gives me "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." what is wrong with it? could you provide an example structure of how the image source sentence would look like? i have this maleperson='<image width=100 src="'+"C:\work\mp.jpg"+'">'"
I m pretty sure this is wrong. how would you write it?
Here are some points to think about:
- The code you uploaded does not have the correct quotes in the final line. (Needs a triple quote at the end.) Please make sure the version you look at or show people when you have a problem is exactly the same code that generated the error message.
- When corrected, it runs without the error message you mention.
- If you ever do get that error message again, instead of panicking, look at the line after the one you quote. It will tell you the name of the offending function. Nine times out of ten, that will be enough for you to realize what is wrong, since you should know what types of arguments that function is supposed to take.
- The first line does not have the arguments that the assignment requires. Read the assignment. (I have already answered one question about this above.)
- If you did have the correct arguments, five of them would be string variables that represent file names. Don't hard code file names into your code. How on earth do you think that your TA, running your program on his or her computer, is going to be able to get to the pictures on your C: drive??
- You are writing a file somewhere, but not in the directory that the assignment requires.
- You are only writing the doctype, not the rest of the HTML.
Colin Potts
I get error message after error message. I have no idea what is wrong with this. could you give me your input.
def makeMadLib(dir,maleperson,femaleperson,pet,place,food):
file=open("hellox.html","wt")
maleperson='<image width=100 src="'+maleperson+'"">'
femaleperson= '<image width=200 src="'+femaleperson+'"">'
pet='<image width=50 src="'+pet+'">'
place='<image width=50 src="'+place+'">'
food='<image width=50 src="'+food+'"">'
file.write("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transition//EN" "http://wwww.w3.org/TR/html4/loose.dtd">""")
<html>
<head>
<title>hello.html</title>
</head>
<body><h1>your story</h1>
- I see eight lines of valid Python followed by five lines of HTML. If you try to load this code you should get ONE message: "The error is on line 9". So look at line 9. It's not valid Python.
- If I delete the final HTML lines and try to run the 8 lines of Python, I don't get any error messages at all. The program isn't doing what it's supposed to do, but it doesn't generate error messages. You could be calling it in the command area with the wrong number of arguments (try 'makeMadLib(pickAFolder(),"1","2","3","4","5") ')
- You are passing in a parameter called dir that you aren't using. Shouldn't this be the name of the directory that you are writing the file to? So why not write there? Where, exactly, is your function trying to write to?
- You're making a bunch of HTML image tags that you probably want to write out into the HTML file. But you aren't doing that.
- If you "have no no idea what is wrong with this", then I am afraid that you should have started this assignment earlier and gone to a TA's office hours with some specific questions. You're essentially asking how to do the assignment, and that's not what this page is for. I don't think that this homework is the issue. Come and see Mark or me during office hours next week and let one of us discuss with you how you are doing in general.
Colin Potts
How on earth do you make the picture bigger. It just keeps giving me a stupid >" I feel like I've tried everything. how do I call "+male+" in two sises???
Link to this Page