[Python] This is a file input problem

Asked 2 years ago, Updated 2 years ago, 18 views

I want to enter 5 different random numbers in the text file, but I can only do that. I don't know how to fix it, so can I ask for some advice? It's only been three weeks since I learned Python, so I don't know what it is.ㅠ<

python

2022-09-22 20:55

2 Answers

import random
# Five random integers are pre-made and stored.
randomNums = (str(random.randint(1, 500)) for _ in range(5))

with open('random.txt', 'w') as wf:
    '''
    1
    2
    3
    A string is created and stored in line units as shown in .
    '''
    wf.write('\n'.join(randomNums))    


2022-09-22 20:55

Write the code with num = random.radint (1,500) and the random value you received at the end (here, 184) goes in 5 times. Define num as a list, not a variable, and num.I think you can get it by append.


2022-09-22 20:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.