The assignments will be graded out of 100 points. Create a text document entitled answers.xxx (where you replace xxx with whatever extension is appropriate, depending on the file format you use). Acceptable file formats are plain text, Word document, OpenOffice document, and PDF. Put your name and UTA ID at the top. Your answers that are not part of a program will be added to this file. Each task below will instruct you where to put your answers.
The following code is supposed to ask the user for a string S
and print the length of
S
as well as the first and last characters. It contains multiple errors. In your
answers.xxx file, explain exactly what the errors are. Do not simply repeat any Python error
message or just give a correction. Note that an error may be a conceptual error, not an error that
causes the program to crash. Then, write a corrected version of the program and save as task1.py.
S = input('Enter a string: ') length = len(S) first_character = S[1] last_character = S[length] print('Length:', length) print('First:', first_character) print('Last:', last_character)
The following code is supposed to:
s
.
s
to a list called l
.
l
on a separate line.
s = 'This isn't a number' l = list(s) for char in l: print(l[char])
Write a program that:
text
.
text
, with all occurrences of letters p, q, r, s, t (both lower-case and upper-case) removed.
For example:
hello wold
.
oday i i ueday
.
Save the code in file task3.py.
Write a program that:
text
.
text
, how many times it occurs in text
.
For example:
h: 1 e: 1 l: 3 o: 2 : 1 w: 1 r: 1 d: 1 !: 1
t: 3 o: 1 d: 2 a: 2 y: 2 : 3 i: 2 s: 2 u: 1 e: 1
Save the code in file task4.py.
text
.
True
if each character in text
is either white space (you can use the isspace
method of strings for that), a dot (character "."), a minus sign (character "-"), or a digit (0, 1, 2, 3, 4, 5, 6, 7, 8, 9).
False
otherwise, i.e., if any character in text
is neither white space, nor a dot, nor a digit.
True
.
True
.
False
, since it contains letters.
False
, since it contains punctuation.
Save your code in file task5.py.
text
.
True
if the string has zero or one dot characters ("." characters).
False
otherwise, i.e., if text
contains two or more dots.
For example:
True
.
True
.
True
.
False
, since it contains two dots.
text
.
True
if the string has no white space characters between non-white-space characters.
False
otherwise, i.e., if text
contains one or more white-space characters between non-white-space characters (again, you can use the string method isspace
to check if a character is white space).
For example:
True
.
True
, since all white space characters are at the beginning or the end.
True
, since all white space characters are at the beginning or the end.
False
, since there is white space between "32" and "14".
False
, since there is white space between "number" and "32.14"
False
, since there are several cases where white space appears between non-white-space characters.
text
.
True
if the string contains a valid float number, and can be safely be cast to a float using the float
function.
False
otherwise, i.e., if the string does not contains a valid float number, and float(text)
produces an error message.
For example:
True
.
True
.
False
, since float(" a.32!#.14 ")
gives an error message.
False
, since float(" 32 14 ")
gives an error message.
False
, since float("number 32.14")
gives an error message.
False
, since float(" 44.21.12 ")
gives an error message.
The assignment should be submitted via Blackboard. Submit a ZIPPED directory called assignment4.zip (no other forms of compression accepted, contact the instructor or TA if you do not know how to produce .zip files). The zipped directory should contain your answers.xxx document and all the Python code files (task5.py, task6.py, etc).