Home > OS >  create a program that modifies multiple sentences entered to have the first letter capitalized pytho
create a program that modifies multiple sentences entered to have the first letter capitalized pytho

Time:04-09

The issues with this code are as following :

  1. What I input is cut off after the first sentence.

  2. When asking the user if they would like to run the program again the code fails to run again.

  3. When other input is entered the program does not end. After the code below I have pasted how it looks when I run the program.


def main ():
    string=input('Enter sentences to be modified:') #enter sentences
    sentence=string.split('.')
    for i in sentence:
        print ("Your modified setences is:"   i.strip().capitalize() ". ",end='')
        print ()
        rerun ()
        
def rerun ():
    while True:
        again = input("Run again? ") #asks user if they want to run again
    if 'y' in again:
        main ()  #reruns def main
    else:
        print("bye") #ends program 
      
main()

Code returns:

Enter sentences to be modified:yes. hello
Your modified setences is:Yes. 
Run again? y
Run again? n
Run again? 

CodePudding user response:

You need to break out of the while loop if it they say no, you have no break, so it will never end. And your if statement was not under the while...

def run():
    string=input('Enter sentences to be modified:') #enter sentences
    sentence=string.split('.')
    for i in sentence:
        print ("Your modified setences is:"   i.strip().capitalize() ". ",end='')
        
def main():
    while 1:
        run()
        again = input("Run again? Y/N ") #asks user if they want to run again
        if again.lower() != 'y': #clean break on N
            break
      
main()

CodePudding user response:

while True: will run forever, because the if 'y' in again and else statements are outside the loop. You need to indent that expression as well! In Python, indentation is extremely important.

CodePudding user response:

You have defined your main() function to be recursive when it doesn't have to be. Instead, try:

def main ():
    while True:
        string = input('Enter sentences to be modified: ')
        sentence = string.split('.')
        for i in sentence:
            print("Your modified setences is: "   i.strip().capitalize()   ". ")
            print()
        while True:
            again = input("Run again? ")
            if 'y' in again:
                break # breaks out of inner while loop, not outer
            else:
                return print("bye") # prints "bye" and ends function
            
main()
  • Related