After understanding the sample code in Chapter 1 of the book, I modified it as follows for practice.
--Point: --Use backslash () (Enter "option + " on Mac) --If you use a backslash in a string, the space after the line break will be output as it is. --You can use a backslash between two line breaks to concatenate without spaces.
sample_01.py
import random
#Define a list
subjects = ['I\
Is','hole\
Or']
verbs = ['Like'\
'is',
'Hate'\
'is']
nouns = ['Summer is','Autumn']
#Select one element from the list
subject = random.choice(subjects)
verb = random.choice(verbs)
noun = random.choice(nouns)
#Concatenate words to make a phrase
phrase = subject + ' ' + noun + ' ' + verb
#Output phrase
print(phrase)
#One of the output results
#You like autumn
Recommended Posts