Strings: making HTML tags: Difference between revisions
No edit summary |
|||
(35 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[File:square.png|right|frame|This a problem set for you to work through <ref>http://www.flaticon.com/</ref>]] | |||
This is a problem set. Some of these are easy, others are far more difficult. The purpose of these problems sets are to HELP YOU THINK THROUGH problems. The solution is at the bottom of this page, but please don't look at it until you have tried (and failed) at least three or four times. | This is a problem set. Some of these are easy, others are far more difficult. The purpose of these problems sets are to HELP YOU THINK THROUGH problems. The solution is at the bottom of this page, but please don't look at it until you have tried (and failed) at least three or four times. | ||
== The | == The Problem == | ||
This problem is designed to test your | This problem is designed to test your skill and knowledge of [[string]]s, specifically your knowledge and understanding of combining (concatenating) strings. | ||
: The web is built with HTML strings like "<i>Yay</i>" which draws Yay as italic text. In this example, the "i" tag makes <i> and </i> which surround the word "Yay". Given tag and word strings, create the HTML string with tags around the word <ref>http://codingbat.com/prob/p132290</ref> | : The web is built with HTML strings like "<syntaxhighlight lang="python" inline><i>Yay</i></syntaxhighlight>" which draws Yay as italic text. In this example, the "i" tag makes <i> and </i> which surround the word "Yay". Given tag and word strings, create the HTML string with tags around the word <ref>http://codingbat.com/prob/p132290</ref> | ||
== | == Some Code to Get You Started == | ||
<syntaxhighlight lang="python" line="1" > | <syntaxhighlight lang="python" line="1" > | ||
Line 20: | Line 22: | ||
# example input: | # example input: | ||
# make_tags('i','Hello world') | # make_tags('i','Hello world') | ||
# example output: | # example output: | ||
# <i>Hello world</i> | # <i>Hello world</i> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | == Take This Farther == | ||
This simple function is very useful, and it is likely that you have used a function like this to add bold face text to a google doc. | |||
* Please create a function that creates a link. <syntaxhighlight lang="python" inline><a href="http://reddit.com/r/python">Click here</a></syntaxhighlight>. You will need the link and text to display to the user. | |||
* Add some logic which rejects [http://www.w3schools.com/tags/ref_html_dtd.asp invalid HTML tags] | |||
== How you will be assessed == | |||
Every problem set is a formative assignment. [[Media:Problem-setrubric.pdf|Please click here to see how you will be graded]] | |||
== References == | == References == | ||
<references /> | |||
== One Possible Solution == | |||
<div class="toccolours mw-collapsible mw-collapsed"> | |||
Click the expand link to see one possible solution, but NOT before you have tried and failed! | |||
<div class="mw-collapsible-content"> | |||
<syntaxhighlight lang="python" line="1" > | |||
# | |||
# this function returns a HTML opening and closing pair of tag | |||
# | |||
def make_tags(tag, word): | |||
print("<" + tag + ">" + word + "</" + tag + ">") | |||
return | |||
# example input: | |||
# make_tags('i','Hello world') | |||
# example output: | |||
# <i>Hello world</i> | |||
</syntaxhighlight> | |||
</div> | |||
</div> | |||
[[Category:problem set]] | |||
[[Category:python]] | |||
[[Category:strings]] |
Latest revision as of 09:52, 17 March 2016
![](/images/d/d1/Square.png)
This is a problem set. Some of these are easy, others are far more difficult. The purpose of these problems sets are to HELP YOU THINK THROUGH problems. The solution is at the bottom of this page, but please don't look at it until you have tried (and failed) at least three or four times.
The Problem
This problem is designed to test your skill and knowledge of strings, specifically your knowledge and understanding of combining (concatenating) strings.
- The web is built with HTML strings like "
<i>Yay</i>
" which draws Yay as italic text. In this example, the "i" tag makes and which surround the word "Yay". Given tag and word strings, create the HTML string with tags around the word [2]
Some Code to Get You Started
#
# this function returns a HTML opening and closing pair of tag
#
def make_tags(tag, word):
return
# example input:
# make_tags('i','Hello world')
# example output:
# <i>Hello world</i>
Take This Farther
This simple function is very useful, and it is likely that you have used a function like this to add bold face text to a google doc.
- Please create a function that creates a link.
<a href="http://reddit.com/r/python">Click here</a>
. You will need the link and text to display to the user. - Add some logic which rejects invalid HTML tags
How you will be assessed
Every problem set is a formative assignment. Please click here to see how you will be graded
References
One Possible Solution
Click the expand link to see one possible solution, but NOT before you have tried and failed!
#
# this function returns a HTML opening and closing pair of tag
#
def make_tags(tag, word):
print("<" + tag + ">" + word + "</" + tag + ">")
return
# example input:
# make_tags('i','Hello world')
# example output:
# <i>Hello world</i>