2016-08-01 9 views
0

Hier ist mein Code. Ich bin sehr neu bei Python und bevor ich zum Bau von Werkzeugen überspringe, möchte ich die Grundlagen kennen. Ist das ein guter Code? Gibt es eine Möglichkeit, es zu verbessern? Heres die Sache, die die .replace() musste 4 mal verwendet werden. Gibt es eine Möglichkeit, ADJECTIVE, ADVERB, NOUN und VERB auf einmal durch Benutzereingaben zu ersetzen? Hinweis: Ich lese AUTOMATISIEREN DAS BOHREN MATERIAL MIT PYTHON praktische Programmierung für Anfänger insgesamt Seite: 195 Mad Libs. Auch ich bin auf Linux;)Gibt es eine Alternative zu .replace() in Python?

#!/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) 

Antwort

0

Ihr Code sieht tatsächlich ziemlich gut aus. Diese Frage wäre wirklich passender auf https://codereview.stackexchange.com/

Hier ist, was ich tun könnte ... in Ihrem ursprünglichen Code hatten Sie tatsächlich einen Fehler - Sie durchschlugen alle Dateien in Ihrem Verzeichnis, aber Sie hatten nur mit der Text in der letzten Datei. Dies verschiebt einige Dinge ein wenig, aber funktioniert tatsächlich für mehrere Dateien in einem Verzeichnis.

#!/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) 
+0

sehr, sehr nett. schätze diesen Link und diesen Code! – wetw0rk

0

Keine 4 ersetzen Anweisungen. Es kann auf final_text=contents_of_the_file.replace('ADJECTIVE', ADJECTIVE).replace('NOUN', NOUN).replace('ADVERB', ADVERB).replace('VERB', VERB)

reduziert werden Sie können auch versuchen, Regex zum Ersetzen mehrerer Zeichenfolgen zu verwenden.

+0

Danke, ich werde wieder mit der Regex spielen. Du hast meine Frage erstaunlich beantwortet! – wetw0rk

+0

Regex ersetzen würde nicht anders funktionieren, und wäre nur anfälliger für Fehler. –

+0

Tatsächlich verwenden Sie immer noch 4 Replace-Anweisungen. –

Verwandte Themen