-4

How to use random numbers and concatenating a string to another string random_number amount of times in python 3 ????

import random
fave_word = 'hello' + 'world'

def randomword(fave_word : str) -> str:
    '''Return something'''
    rand_num = random.randint(0,3)
    for i in range(rand_num):
        fave_word += 'Bob'
print(fave_word)
cs95
  • 379,657
  • 97
  • 704
  • 746
M.S
  • 1

1 Answers1

0

You need to return the string you've created. Or you could use global, but I'd not recommend that approach:

>>> import random
>>> fave_word = 'hello' + 'world'
>>>
>>> def random_word(word : str) -> str:
...     '''Return something'''
...     rand_num = random.randint(0,3)
...     for i in range(rand_num):
...         word += 'Bob'
...     return word
...
>>> print(random_word(fave_word))
helloworldBobBobBob
>>> print(random_word(fave_word))
helloworld
>>> print(random_word(fave_word))
helloworldBobBob
import random
  • 3,054
  • 1
  • 17
  • 22