This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Write a program to read through a file and print the contents of the file (line by line) all in upper case. Executing the program will look as follows: | |
python shout.py | |
Enter a file name: mbox-short.txt | |
FROM [email protected] SAT JAN 5 09:14:16 2008 | |
RETURN-PATH: <[email protected]> | |
RECEIVED: FROM MURDER (MAIL.UMICH.EDU [141.211.14.90]) | |
BY FRANKENSTEIN.MAIL.UMICH.EDU (CYRUS V2.3.8) WITH LMTPA; | |
SAT, 05 JAN 2008 09:14:16 -0500 | |
''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Take the following Python code that stores a string: | |
# str = 'X-DSPAM-Confidence:0.8475' | |
# Use find and string slicing to extract the portion of the string after the colon character and then use the float function to convert the extracted string into a floating point number. | |
data = input('Enter your text: ') | |
col_pos = data.find(':') | |
flt_find = data[col_pos+1:] | |
flt_find = float(flt_find) | |
print (flt_find) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Exercise 3: Encapsulate this code in a function named count, and generalize it so that it accepts the string and the letter as arguments | |
def count(word, letter): | |
count = 0 | |
for character in word: | |
if character == letter: | |
count = count + 1 | |
print (count) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Exercise 1: Write a while loop that starts at the last character in the string and works its way backwards to the first character in the string, printing each letter on a separate line, except backwards. | |
fruit = 'banana' | |
index = len(fruit) - 1 | |
while index >= 0 : | |
letter = fruit[index] | |
print(letter) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
largest = -1 | |
smallest = None | |
while True : | |
num = input('Enter a number: ') | |
if num == "done" : | |
break | |
try: |