Hello I’m learning Python with automatetheboringstuff’s work by Al Sweigart, here are the instructions for the exercise copy and paste directly from the website :
Mad Libs
Create a Mad Libs program that reads in text files and lets the user add their own text anywhere the word ADJECTIVE, NOUN, ADVERB, or VERB appears in the text file. For example, a text file may look like this:
The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was
unaffected by these events.
The program would find these occurrences and prompt the user to replace them.
Enter an adjective:
silly
Enter a noun:
chandelier
Enter a verb:
screamed
Enter a noun:
pickup truck
The following text file would then be created:
The silly panda walked to the chandelier and then screamed. A nearby pickup
truck was unaffected by these events.
The results should be printed to the screen and saved to a new text file.
Here is my code :
import re
file_source_path = r"C:UsersxavieDesktopFichier PYTHON + MUFichier Mutextpoubelle.txt"
file_source = open(file_source_path, "r")
print("Here is the text with the occurrences to be changed :n ")
content = file_source.read()
file_source.close()
print(content)
adj = input("Change the adjective :n")
nameRegex1 = re.compile(r"ADJECTIVE")
phrase1 = nameRegex1.sub(adj, content)
print(phrase1)
content1 = phrase1
nom1 = input("Change the name :n")
nameRegex1 = re.compile(r"NOUN")
phrase2 = nameRegex1.sub(nom1, content1, count=1)
print(phrase2)
content2 = phrase2
verbe = input("Change THE VERB :n")
nameRegex1 = re.compile(r"verb")
phrase3 = nameRegex1.sub(verbe, content2)
print(phrase3)
content3 = phrase3
nom2 = input("Change the second name :n")
nameRegex1 = re.compile(r"NOUN")
phrase4 = nameRegex1.sub(nom2, content3)
print(phrase4)
content4 = phrase4
final_destination = r"C:UsersxavieDesktopFichier PYTHON + MUFichier Munewfichier.txt"
fd = open(final_destination, "w")
fd.write(content4)
fd.close()
print()
print('Save file in "newfichier.txt".')
print()
print("Result :n" + content4)
thanks to those who will try to help me 🙂
submitted by /u/PracticalBox7827
[link] [comments]
r/learnpython Hello I’m learning Python with automatetheboringstuff’s work by Al Sweigart, here are the instructions for the exercise copy and paste directly from the website : Mad Libs Create a Mad Libs program that reads in text files and lets the user add their own text anywhere the word ADJECTIVE, NOUN, ADVERB, or VERB appears in the text file. For example, a text file may look like this: The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events. The program would find these occurrences and prompt the user to replace them. Enter an adjective: silly Enter a noun: chandelier Enter a verb: screamed Enter a noun: pickup truck The following text file would then be created: The silly panda walked to the chandelier and then screamed. A nearby pickup truck was unaffected by these events. The results should be printed to the screen and saved to a new text file. Here is my code : import re file_source_path = r”C:UsersxavieDesktopFichier PYTHON + MUFichier Mutextpoubelle.txt” file_source = open(file_source_path, “r”) print(“Here is the text with the occurrences to be changed :n “) content = file_source.read() file_source.close() print(content) adj = input(“Change the adjective :n”) nameRegex1 = re.compile(r”ADJECTIVE”) phrase1 = nameRegex1.sub(adj, content) print(phrase1) content1 = phrase1 nom1 = input(“Change the name :n”) nameRegex1 = re.compile(r”NOUN”) phrase2 = nameRegex1.sub(nom1, content1, count=1) print(phrase2) content2 = phrase2 verbe = input(“Change THE VERB :n”) nameRegex1 = re.compile(r”verb”) phrase3 = nameRegex1.sub(verbe, content2) print(phrase3) content3 = phrase3 nom2 = input(“Change the second name :n”) nameRegex1 = re.compile(r”NOUN”) phrase4 = nameRegex1.sub(nom2, content3) print(phrase4) content4 = phrase4 final_destination = r”C:UsersxavieDesktopFichier PYTHON + MUFichier Munewfichier.txt” fd = open(final_destination, “w”) fd.write(content4) fd.close() print() print(‘Save file in “newfichier.txt”.’) print() print(“Result :n” + content4) thanks to those who will try to help me 🙂 submitted by /u/PracticalBox7827 [link] [comments]
Hello I’m learning Python with automatetheboringstuff’s work by Al Sweigart, here are the instructions for the exercise copy and paste directly from the website :
Mad Libs
Create a Mad Libs program that reads in text files and lets the user add their own text anywhere the word ADJECTIVE, NOUN, ADVERB, or VERB appears in the text file. For example, a text file may look like this:
The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was
unaffected by these events.
The program would find these occurrences and prompt the user to replace them.
Enter an adjective:
silly
Enter a noun:
chandelier
Enter a verb:
screamed
Enter a noun:
pickup truck
The following text file would then be created:
The silly panda walked to the chandelier and then screamed. A nearby pickup
truck was unaffected by these events.
The results should be printed to the screen and saved to a new text file.
Here is my code :
import re
file_source_path = r"C:UsersxavieDesktopFichier PYTHON + MUFichier Mutextpoubelle.txt"
file_source = open(file_source_path, "r")
print("Here is the text with the occurrences to be changed :n ")
content = file_source.read()
file_source.close()
print(content)
adj = input("Change the adjective :n")
nameRegex1 = re.compile(r"ADJECTIVE")
phrase1 = nameRegex1.sub(adj, content)
print(phrase1)
content1 = phrase1
nom1 = input("Change the name :n")
nameRegex1 = re.compile(r"NOUN")
phrase2 = nameRegex1.sub(nom1, content1, count=1)
print(phrase2)
content2 = phrase2
verbe = input("Change THE VERB :n")
nameRegex1 = re.compile(r"verb")
phrase3 = nameRegex1.sub(verbe, content2)
print(phrase3)
content3 = phrase3
nom2 = input("Change the second name :n")
nameRegex1 = re.compile(r"NOUN")
phrase4 = nameRegex1.sub(nom2, content3)
print(phrase4)
content4 = phrase4
final_destination = r"C:UsersxavieDesktopFichier PYTHON + MUFichier Munewfichier.txt"
fd = open(final_destination, "w")
fd.write(content4)
fd.close()
print()
print('Save file in "newfichier.txt".')
print()
print("Result :n" + content4)
thanks to those who will try to help me 🙂
submitted by /u/PracticalBox7827
[link] [comments]