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.
CodePudding user response:
Python String capitalize() method returns a copy of the original string and converts the first character of the string to a capital (uppercase) letter, while making all other characters in the string lowercase letters.
Key word here is "Returns". You're using capitalize method (a sort of special function, it will become clear in the class paragraph) but you're not using the value it returns.
frase = frase.capitalize()
print(f"{frase}\n")
CodePudding user response:
I've re-written your code to improve the structure and layout. Note - use Lists not numpy arrays for text. Note use of random.choice.
import random
articolo = ['il', 'lo', 'la', 'gli', 'le', 'un', 'uno', 'una']
nome = ['fagotto', 'coccinella', 'puntale', 'piastrella', 'serra', 'magia', 'miope', 'curvo']
verbo = ['amare', 'tornare', 'sapere', 'piacere', 'dare', 'fare', 'essere', 'leggere']
preposizione = ['di', 'a', 'da', 'in', 'con', 'su', 'per', 'tra']
for i in range(20):
frase = ''
for j in [articolo, nome, verbo, preposizione, articolo, nome]:
parola = random.choice(j)
frase = parola ' '
frase = '.'
frase = frase.capitalize()
print(f"{frase}\n")