繁体   English   中英

如何询问用户是否要重新运行程序

[英]How to ask if the user wants to re-run the program

我不知道如何询问用户是否要再次重新启动程序并输入新文件。 我想问一下用户是要重新运行程序还是只是终止它。 最简单的方法是什么? 该程序如下:

import nltk
from nltk.tokenize import word_tokenize
import re
import os
import sys
from pathlib import Path




data_folder = Path(input("type the path you would like to use: "))
file_to_open = data_folder / input("insert the file you would like to   use with its extension: ")
with open(file_to_open) as f:
    words = word_tokenize(f.read().lower())
with open ('Fr-dictionary2.txt') as fr:
    dic = word_tokenize(fr.read().lower())


l=[ ]
errors=[ ]
out_file=open("newtext.txt","w")

for n,word in enumerate (words):
    l.append(word)
    if word == "*":
        exp = words[n-1] + words[n+1]
        print("\nconcatenation error:", exp)
        if exp in dic:
            l.append(exp)
            l.append("$")
            errors.append(words[n-1])
            errors.append(words[n+1])
        else:
            continue


for i, w in enumerate(l):
    if w == "*":
        l.remove(l[i-1])
    else:
        continue
for i, w in enumerate(l):
    if w == "$":
        l.remove(l[i+1])
    else:
        continue

text=' '.join(l)
print('\n\n',text)
e=len(errors)
print('\n',e/2,'WORDS CONCATENATED IN TEXT',errors)

user=input('\nREMOVE * AND $ FROM TEXT? Type "Y" for yes or "N" for   no:')


for x in l:
    if user=='Y' and x=='*':
        l.remove(x)
    elif user=='Y' and x=='$':
        l.remove(x)
    else:
        continue

final_text=' '.join(l)
print('\n\n', final_text)

user2=input('\nWrite text to a file? Type "Y" for yes or "N" for no:')


if user2 =='Y':
    out_file.write(final_text)
    out_file.close()
    print('\nText named "newtext.txt" written to a file'

这是您正在寻找的基本结构:

def some_function_you_want_to_repeat():
    print("Do some stuff")
    choice = input("Do you want to do this again? | yes/no")
    if choice == 'yes':
        some_function_you_want_to_repeat()


some_function_you_want_to_repeat()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM