我如何在python中重复该程序

时间:2016-12-28 16:20:36

标签: python

我无法编写程序来重复此程序。我想问他们是否想重复(是或否)。如果他们说是,则重复整个计划。这就是我到目前为止所做的:

sentence=input("Please enter sentence(s)")
words = sentence.split()
number_of_words = len(words)
counter=0
for x in sentence:
    if x in "!?.":
        counter=counter+1
print("There is "+str(counter)+" sentences and " + str(number_of_words) + "  words")

2 个答案:

答案 0 :(得分:1)

组织代码的一种好方法是将主程序放入名为var counter = 0; var max = 100000; function myPrint(){ if(counter < max){ console.log(counter++); requestAnimationFrame(myPrint); } } myPrint(); 或类似的函数中:

main()

然后,在此下面,编写代码以重复该函数:

def main():
    sentence = input("Please enter sentence(s): ")
    num_words = len(sentence.split(' '))

    counter = 0
    for x in sentence:
        if x in "!?.":
            counter += 1

    print("There are", counter, "sentences and", num_words, "words.")

答案 1 :(得分:0)

将整个代码放在一个True循环中,最后询问用户是否要重复。如果不打破循环。

类似的东西:

while True:
    sentence=input("Please enter sentence(s)")
    words = sentence.split()
    number_of_words = len(words)
    counter=0
    for x in sentence:
        if x in "!?.":
            counter=counter+1
    print("There is "+str(counter)+" sentences and " + str(number_of_words) + "words")
    if input('Do you want to repeat(y/n)') == 'n':
        break