[Python] Simple tweet generator
Hello guys,
this time, i have a simple tweed generator for you.
What is this generator doing?
It creates a sentence out of four lists "part1 to part4", each containing a subsentence.
What does this code snippet teach about?
Output:
To get a better understanding of how it works, i would suggest to take this code snippet and play around with it.
For example, you could try to add new parts of subsentences.
this time, i have a simple tweed generator for you.
What is this generator doing?
It creates a sentence out of four lists "part1 to part4", each containing a subsentence.
What does this code snippet teach about?
- lists
- encapsulated lists
- string manipulation via "join"
- the "in"-operator
- the "for"- loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # Import random module import random # Defining lists part1 = ["Angelika Merkel,", "Frank-Walter Steinmeier,", "Wolfgang Schäuble,", "Joschka Fischer,", "Oskar Lafontaine,", "Gerhard Schröder,", "Ursula von der Leyen,", "Renate Künast,", "Sigmar Gabriel,"] part2 = ["ohne Talent,", "völlig blau,", "total besoffen,", "absolut unzurechnungsfähig,", "nichts ahnend,", "sehr verstört,"] part3 = ["hatte einen Rausch.", "lief gegen einen Baum.", "stolperte die Treppe hinunter.", "fiel aus dem Bett.", "rutschte ins Klo.", "aß Hundefutter zum Frühsück.", "ging im Winter baden.", "wärmte sich die Hände im Toaster."] part4 = ["Unglaublich!", "Schlimme Konsequenzen!", "Wie befreiend!", "So wahr!", "So natürlich!", "Eine Wohltat!"] # Defining list of parts all_parts = [part1, part2, part3, part4] # Declaring an empty list sentence = [] # Loop through all parts for part in all_parts: # Generate a random number according to part's length s = random.randint( 0, len(part)-1 ) # Appending the chosen words sentence.append( part[s] ) # Printing the new generated sentence print( " ".join(sentence) ) |
Output:
Angelika Merkel, sehr verstört, aß Hundefutter zum Frühsück. Unglaublich!
To get a better understanding of how it works, i would suggest to take this code snippet and play around with it.
For example, you could try to add new parts of subsentences.
Comments
Post a Comment