I am having trouble writing a variable named UpperCaseSentence outside of a for loop in this particular code snippet. It runs fine while in the loop but not while out of the loop. Can anyone shed some light on this dilemma for me. Here is the code.
Thank you a bunch.

#Main function.
def main():

    mySentence = (input("Enter text."))

    mySentenceList = mySentence.split('.')

    #Call fixCase function. Send it mySentenceList and receive result
    #and stores result in variable named output.
    output = fixCase(mySentenceList)

def fixCase(myList):
    #Begin making a loop through the list, using a variable myString
    for myString in range (len(myList)):
        tempString = myList[myString] #Store in temporary variable.
        myList[myString] = tempString[0:1].upper() + tempString[1:len(tempString)] #Replace with upper
        UpperCaseSentence = (myList[myString])

#Call main function

Recommended Answers

All 7 Replies

I have tested various for loops of this same flavor with favourable results. I must be overlooking something simple.

There is no return statement in function fixcase(), so that your variable output will always be None.

Also have a look at the capitalize() string method.

Hello Gribouillis,
I tried the code with a return statement before posting this question and it still did not help me with the for loop problem. Sorry for the confusion but I am only worried about the variable UpperCaseSentence not being able to work outside of the for loop.

UpperCaseSentence = (myList[myString])

UpperCaseSentence is a tuple that only contains the final item in the list "myList", as you have written it, which is an empty string if the text that is entered ends with a period.

mySentenceList = mySentence.split('.')

Add some print statements and see for yourself. Also, as Gribouillis mentioned above, UpperCaseSentence can not be seen outside of the function in the code you posted.

Hello woooee,
I think I understand what you are referring to I will give it a try. I should have done more testing.
Also, I am not worried about the return statement now. I am just trying to get past the for loop which I think you have clarified for me.
Thank You,

You are so right. If I replace a period with a question mark the statement outside the loop works.
Thank you sir.

Here is the psuedo code for the program that I am trying to write. I do not seek but some good advice about what I am doing with this program.
Thank You,

#This program will receive input and replace the first letter in each sentence with an uppercase letter.

def main():
  mySentence = "hello. my name is Joe. what is your name. where are you from."
#use the split() method to split mySentence by period into a list, example mySentenceList
#send mySentenceList to a function (example fixCase) that returns a string, store that string to a variable, exaple output
#print the variable output
def fixCase(myList):
1) #Begin make a loop through the list, using a variable, example myString
for myString in range (len(myList)): #this is actual code to type
a) #store the first element to a temporary variable, #example tempString equals myList[ ... you fill this out ..]
b) # replace the first element of myList with the uppercase ... as you loop you will do the next #element - play around with this looping and print out each element like examples show in # the book. See actual code for this step below.
#this next line is actual code to type myList [myString] = tempString[0:1].upper() + tempString[1:len(tempString)]
2) #declare a variable to store the upper case sentence, example myUpperCaseSentence. Be sure you out dent, you do not want to be in the loop now.
3) start a new loop, concatenate each element of myList to the myUpperCaseSentence string. Look at string concatenation!
for myString in range (len(myList)): #this is actual code to type
# write a line to concatenate each element to the myUpperCaseSentence variale, hint # myUpperCaseSentence += ?
4) return myUpperSentence -#this should not be under any loop, out dent.

main ()

EDIT: pseudocode must also be in code block, and this is allmost ready program

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.