How Do I Repeat The Program In Python
I am having trouble writing a program to repeat this program. I want to ask them if they want to repeat (yes or no). If they say yes it repeats the whole program. This is what I ha
Solution 1:
A good way of organizing your code is to put your main program into a function called main()
or similar:
def main():
sentence = input("Please enter sentence(s): ")
num_words = len(sentence.split(' '))
counter = 0
for x in sentence:
if x in "!?.":
counter += 1print("There are", counter, "sentences and", num_words, "words.")
Then, underneath this, you write your code for repeating the function:
while True:
main()
ifinput("Repeat the program? (Y/N)").strip().upper() != 'Y':
break
Solution 2:
Put the whole code in a while True
loop and at the end ask the user if they want to repeat. If not, break the loop.
Something like this:
whileTrue:
sentence=input("Please enter sentence(s)")
words = sentence.split()
number_of_words = len(words)
counter=0for x in sentence:
if x in"!?.":
counter=counter+1print("There is "+str(counter)+" sentences and " + str(number_of_words) + "words")
ifinput('Do you want to repeat(y/n)') == 'n':
break
Post a Comment for "How Do I Repeat The Program In Python"