[英]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.