Write a program that gets text from the user Count the number of uppercase letters and print this out Ex Hi There gt 2 uppercase letters Solution 1 text rawinput Please enter some text ID: 580098
Download Presentation The PPT/PDF document "Practice 1" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Practice 1
Write a program that gets text from the user. Count the number of uppercase letters and print this out.
Ex: “Hi There!” -> “2 uppercase letters”Slide2
Solution 1
text =
raw_input
("Please enter some text: ")
upper_count
= 0
for
i
in range(0,
len
(text), 1):
if
text[i].lower
() !=
text[i
]:
# if
text[i].upper
() ==
text[i
]:
upper_count
+= 1
p
rint "I found",
upper_count
, "upper case letters. "Slide3
Solution 2
text =
raw_input
("Please enter some text: ")
upper_count
= 0
for letter in text:
if letter >=
'
A
'
and letter <=
'
Z
'
:
upper_count
+= 1
print "I found",
upper_count
, "upper case letters. "Slide4
Compare solution 1 & 2
for
i
in range(0,
len
(text), 1):
if text[
i
].lower() != text[
i
]:
upper_count
+= 1
for letter in text:
if letter >=
'
A
'
and letter <=
'
Z
'
:
upper_count
+= 1Slide5
Practice 2
“The farm” book
The Farm by Mary Fried
Here is the pig. oink-oink
Here is the cow. moo-moo
Here is the horse. nee-nee
Here is the farm.
Diaosaurs
by
Nanet
George
I see two dinosaurs.
I see four dinosaurs.
I see six dinosaurs.
I see eight dinosaurs.
I see ten dinosaurs.
A dinosaur family!Slide6
Solution 1 (using ONE list)
animals = (("pig", "oink-oink"), ("cow", "moo-moo"), ("horse", "nee-nee"))
#animals = [["pig", "oink-oink"], ["cow", "moo-moo"], ["horse", "nee-nee"]
]
#using a nested sequence
for animal in animals:
print "Here is the", animal[0] + ". " + animal[1
] +
".
”
p
rint
"
Here is the farm."Slide7
Solution 2 (using TWO lists)
print "The
Fram
by Mary Fried"
animals = ("pig", "cow", "horse")
sounds = ("oink-oink", "moo-moo", "nee-nee"
)
for
idx
in range(
len
(animals)):
print "Here is the " + animals[
idx
] + ". " + sounds[
idx
] + "."
print "Here is the farm."Slide8
What do you get?
print "The
Fram
by Mary Fried"
animals = ("pig", "cow", "horse")
sounds = ("oink-oink", "moo-moo", "nee-nee")
for
animal in animals:
for sound in sounds:
print "Here is the " + animal + ". " + sound + "."
print "Here is the farm."