Discussion Assignment 5 solution PDF

Title Discussion Assignment 5 solution
Course Programming Fundamentals
Institution University of the People
Pages 2
File Size 124.1 KB
File Type PDF
Total Downloads 62
Total Views 536

Summary

This assignment is based on Exercise 8 from your textbook. Each of the following Python functions is supposed to check whether its argument has any lowercase letters.For each function, describe what it actually does when called with a string argument. If it does not correctly check for lowercase let...


Description

This assignment is based on Exercise 8.4 from your textbook. Each of the following Python functions is supposed to check whether its argument has any lowercase letters. For each function, describe what it actually does when called with a string argument. If it does not correctly check for lowercase letters, give an example argument that produces incorrect results, and describe why the result is incorrect.

def any_lowercase1(s): for c in s: if c.islower(): return True else: return False

Result is based only on first letter of string s The if statement make it a Boolean expression, so the first character in the string will be tested and return a Boolean statement

Print(any_lowercase1(‘Judgment’) ) def any_lowercase2(s): for 'c' in s: if c.islower(): return 'True' else: return 'False' print(any_lowercase2(‘JUDGMENT’) ) def any_lowercase3(s): for c in s: flag = c.islower() return flag print(any_lowercase3(‘judgmenT’) ) def any_lowercase4(s): flag = False for c in s: flag = flag or c.islower() return flag print(any_lowerclase4(JUDGMENT))

SyntaxError: can't assign to literal In this function we got a syntaxError because ‘c’ is a string and cannot test islower():

Result is based only on last letter of string s In this function, ‘flag’ is assign (a variable) which mean the first four characters are checked till the last character which is outside the loop.

Works correctly. In this function, ‘flag’ = False and have variable of Boolean expression. the ‘or’ logical operator is False for flag or c.islower()

def any_lowercase5(s): for c in s: if not c.islower(): return False return True print(any_lowercase5(‘judgment’) )

Return True only if all letters in s are lowercase. Because the ‘not’ logical operator makes True to be False, which is a Boolean expression...


Similar Free PDFs