How do I add a \"else\" clause to the loop for list \"a\"? if \"inputString\" ha
ID: 3743627 • Letter: H
Question
How do I add a "else" clause to the loop for list "a"? if "inputString" has no character greater than the "keyChar", I am supposed to return a message to the user. Thank you much!
def char(keyChar, inputString):
a = min([element for element in inputString.lower() if element > keyChar]);
if a==[]:
print ("No characters were larger than "+keyChar)
#print(a);
b= [pos for pos, char in enumerate(inputString) if char == a];
#print(b);
print("In "+(inputString)+ ", the smallest character greater than "+(keyChar)+ " is "+a+ " and occurs at position "+str(b));
Explanation / Answer
def char(keyChar, inputString): lst = [element for element in inputString.lower() if element > keyChar] if len(lst) == 0: print("No characters were larger than " + keyChar) return a = min(lst) # print(a); b = [pos for pos, char in enumerate(inputString) if char == a]; # print(b); print("In " + (inputString) + ", the smallest character greater than " + ( keyChar) + " is " + a + " and occurs at position " + str(b)); char('d', 'abc') char('b', 'abc')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.