This PYTHON QUESTION says : SyntaxError: 'return' outside function(SentenceCapitalizer.py, line 28) 28 return lastcharacter def main(): position
Question:
This PYTHON QUESTION says :
- SyntaxError: 'return' outside function(SentenceCapitalizer.py, line 28)
- 28 return lastcharacter
def main():
position = 1 # Initialize position variable value to"1".
statement = input("Enter sentence to be capitalized:") # Input takes the user entered sentence and stores in"statement" variable.
word = statement.split() # word spliter.
is_capital(word[0]) # is_capital function call to check first wordis capital or not.
lastcharacter = endchecker(word[0]) # calls endchecker function andstores the return value of function in lastcharactervariable.
while position < len(word): # while loop to check the user inputsentence.
if lastcharacter == "True": # If last character istrue than call is_capital function.
is_capital(word[position]) # functioncall with word[postion] as segment.
else: # Else statement
print(word[position],end=' ') # Displays the word inthat position
lastcharacter = endchecker(word[position]) # calls endcheckerfunction with input segment and stores the value in lastcharactervariable.
position += 1 # increment the position value by 1.
print("") # goes to next line
def is_capital(partofsentence): # is_capital functiondefinition
print(partofsentence[:1].upper(),end='') # printfirst letter of word as upper case.
print(partofsentence[1:],end=' ') # print remaining letters of thatword.
def endchecker(partofsentence): # endchecker functiondefinition
lastcharacter = "False" # lastcharacter isfalse
# Check the condition if the sentence contains .,?,! as lastcharacters of a word then end of sentence i.e,lastcharactervariable becomes true and next word first character will becapitalized.
if '.' in partofsentence:
lastcharacter = "True"
elif '!' in partofsentence:
lastcharacter = 'True'
elif '?' in partofsentence:
lastcharacter = 'True'
# function endchecker returns "lastcharacter"
return lastcharacter
main()
Data Structures and Algorithms in Python
ISBN: 978-1118290279
1st edition
Authors: Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser