这是我的代码。我是python的新手,在开始构建工具之前,我想了解基础知识。这是好的代码吗?有什么方法可以改进吗?继承人.replace()必须使用4次。有什么办法可以使用用户输入一次性替换ADJECTIVE,ADVERB,NOUN和VERB吗?注意:我正在阅读POMTHON实用程序设计的初学者页面:195 Mad Libs。我也在linux上;)
#!/usr/bin/env python
# Usage: ./mad_libs.py start
# This program finds any .txt document to look for instances of ADJECTIVE, ADVERB, NOUN, and VERB within the file.
# these instances will be replaced by user input
import sys, os, time
# This opens any .txt file within the current working directory
try:
if sys.argv[1].lower() == 'start':
for file in os.listdir('.'):
if file.endswith('.txt'):
print('Opening text file...')
time.sleep(5)
os.system('clear')
open_file = open(file, 'r')
read_open_file = open_file.read()
contents_of_the_file = str(read_open_file)
# If the user does not run ./mad_libs.py start print this and close the program
except IndexError:
print('''Usage: ./mad_libs.py start
This program grabs a text file in the current working directory. The text file must contain any of the
following; ADJECTIVE, ADVERB, NOUN, VERB in capital letters each. Any instances will be replaced with
user input.
''')
sys.exit()
# This asks the user for an adjective, adverb, noun, and verb
print('Give me an adjective')
ADJECTIVE = raw_input()
print('Give me an adverb')
ADVERB = raw_input()
print('Give me a noun')
NOUN = raw_input()
print('Give me a verb')
VERB = raw_input()
# Anything in the file containing ADJECTIVE, NOUN, ADVERB, and VERB will be replaced with user input
modification_to_file_1 = contents_of_the_file.replace('ADJECTIVE', ADJECTIVE)
modification_to_file_2 = modification_to_file_1.replace('NOUN', NOUN)
modification_to_file_3 = modification_to_file_2.replace('ADVERB', ADVERB)
final_text = modification_to_file_3.replace('VERB', VERB)
# Finished new content is printed to user
os.system('clear')
print(final_text)
答案 0 :(得分:0)
不需要4个替换语句。它可以减少到final_text=contents_of_the_file.replace('ADJECTIVE', ADJECTIVE).replace('NOUN', NOUN).replace('ADVERB', ADVERB).replace('VERB', VERB)
您也可以尝试使用正则表达式替换多个字符串。
答案 1 :(得分:0)
您的代码实际上看起来很不错。这个问题在https://codereview.stackexchange.com/
上更合适以下是我可能会做的...在您的原始代码中,您实际上遇到了错误 - 您正在遍历目录中的所有文件,但您只处理上一个文件中的文本。这会稍微改变一些事情,但实际上会对一个目录中的多个文件起作用。
#!/usr/bin/env python
# Usage: ./mad_libs.py start
# This program finds any .txt document to look for instances of ADJECTIVE, ADVERB, NOUN, and VERB within the file.
# these instances will be replaced by user input
import sys, os, time
# This opens any .txt file within the current working directory
try:
if sys.argv[1].lower() != 'start':
sys.exit()
except IndexError:
# If the user does not run ./mad_libs.py start print this and close the program
print('''Usage: ./mad_libs.py start
This program grabs a text file in the current working directory. The text file must contain any of the
following; ADJECTIVE, ADVERB, NOUN, VERB in capital letters each. Any instances will be replaced with
user input.
''')
sys.exit()
# This asks the user for an adjective, adverb, noun, and verb
print('Give me an adjective')
adjective = raw_input()
print('Give me an adverb')
adverb = raw_input()
print('Give me a noun')
noun = raw_input()
print('Give me a verb')
verb = raw_input()
for file in os.listdir('.'):
if file.endswith('.txt'):
print('Opening text file...')
time.sleep(5)
os.system('clear')
with open(file, 'r') as f:
text = f.read()
for word in (adjective, noun, adverb, verb):
text = text.replace('ADJECTIVE', adjective)
text = text.replace('NOUN', noun)
text = text.replace('ADVERB', adverb)
text = text.replace('VERB', verb)
os.system('clear')
print(text)