1

This is my code:

t=['']*20
slist=s1.split()
for i in range(20):
    j=math.floor(random()*10)
    for k in range(5):
        t[i]=(slist[j])
    print(t[i])

Basically t[i] is a list with a word in it and I want to add 5 random words from slist to t[i]. With just the equal sign it overwrites the word. Also += or append doesn't seem to work in list. I know there are better methods out there but i wanna see if it can be done like this.

1 Answer 1

2

The issue is that strings are immutable.

t=['']*20
slist=s1.split()
for i in range(20):
    j=math.floor(random()*10)
    for k in range(5):
        t[i]= t[i] + slist[j]
    print(t[i])

Just use + and reassign the returned string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.