Help file.write random.simple

Hello there,
I’ve a problem with my code.
I need save in .txt file a variable with random.sample from a List.
Python save my file with this orrible list format .
How I can remove the List format?
I need a simple str on .txt without [ ] .

Ex my .txt

Hello! #correct

–>world!<-- #correct

Today is: #correct
[‘Monday’, ‘10’] #uncorrect



What’s your code so far?

This is my example code; for list house_1 the str is correct, without [ ], but in list house the result is : [‘roof’, ‘room’].
How can I change the code for have this result using random.simple?

# My Code example

import random
house = ['room', 'bath', 'floor', 'roof']
house_1 = ['room', 'bath', 'floor', 'roof']

while True:
    file = open('My house', 'x')
    file.write("\nWelcome to my house\n\n") 

    insert = input('write here --> ')
    if insert == 'yes':
        file.write('hello hello hello\n')

        random_house = random.sample(house, 2)
        random_house_1 = random.choice(house_1)
        file.write(str(random_house) + '\n')

# my result (wrong)

Welcome to my house

hello hello hello
['roof', 'room']

# Example (what I want)

Welcome to my house

hello hello hello

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

sample method returns list, while choice returns just chosen item. Therefore if you’d want to have string in the first case, you’d need to make some additional operation on the result returned by sample method.

What kind of operations should I make?
Just a hint please, I’m stuck

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.