0

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.

Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
Davide
  • 3
  • 1

2 Answers2

1

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")
Cholas
  • 50
  • 7
  • Thanks, this definitely solved my problem. I was actually convinced that the strip() and capitalize() methods directly modify the object on which they are called, but instead they apparently produce views of the objects. – Davide Dec 15 '22 at 10:44
  • Same applies to `.strip()` - it is having no effect. Use `frase = frase.strip()` – user19077881 Dec 15 '22 at 10:44
  • Yup. Since my guess is that you're learning python @Davide, as a thumb rule for every programming language is to read the doc;). Please take my comment as a suggestion and not as an offense. Python is peculiar since the huge quantity of libraries, but for pretty much everyone of them you'll find a documentation explaining what the single functions do, just google "library name doc". – Cholas Dec 15 '22 at 10:58
  • Thanks for your suggestions. Appreciate it – Davide Dec 15 '22 at 16:55
0

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")
user19077881
  • 3,643
  • 2
  • 3
  • 14