I am facing a problem that I found in a study book on Python.
I'll preface this by saying that I haven't studied "classes" yet (I haven't gotten to that chapter yet) and my training is mainly based on loops, functions, lists, arrays, Series and DataFrame, regular expressions.
In the chapter on string handling, I was asked to perform an exercise that randomly fishes words out of 4 different arrays and joins them into a single string, finally changing the first letter of the sentence (putting it in uppercase) and adding a period (.) at the end of the sentence.
Based on the code below, I was only unable to complete the uppercase character and the period.
It would appear that Python does not see the object named "frase" (an italian word which means sentence in english) as a single string, but only as an object composed of a set of characters separated from each other.
How do I convert the "phrase" object to a unique string without using the classes I have not yet studied?
I tell you this since I have only found methods on the Internet that involve the use of classes.
Thank you in advance for any support you can lend me.
A very happy programming to everyone
import random
import numpy as np
frase = ''
articolo = np.array(['il',
'lo',
'la',
'gli',
'le',
'un',
'uno',
'una'])
nome = np.array(['fagotto',
'coccinella',
'puntale',
'piastrella',
'serra',
'magia',
'miope',
'curvo'])
verbo = np.array(['amare',
'tornare',
'sapere',
'piacere',
'dare',
'fare',
'essere',
'leggere'])
preposizione = np.array(['di',
'a',
'da',
'in',
'con',
'su',
'per',
'tra'])
def casuale(arr):
return arr[random.randrange(8)]
for i in range(20):
frase = ''
for j in [articolo, nome, verbo, preposizione, articolo, nome]:
frase += casuale(j) + ' '
frase.strip()
frase += '.'
frase.capitalize()
print(f"{frase}\n")
I tried converting the object with the STR() function but it did not work.