0

Hello i am new to python and i would like your help So i just start learning and the first thing came on my mind is Why not edit a simlpe program ! Well i want to take a line (a username) and put it in an existing code

Here a part of the code

def getRandomUsername():
    username = 'Name_'
    for x in range(0,9):
            username = username + getRandomChar()
    return username

I want to pull from my .txt automatically a username and replace it with the hole thing any ideas ?

As i told im very new to this type of thing also sorry for my bad engls

--

Thanks Guys

Donkey32
  • 3
  • 3

2 Answers2

1

If your text file is not particularly large, you can read the whole thing into memory and select a random one:

import random

# You can do this once in your program
lines = open('filename').read().splitlines()

# Whenever you need a new random line, you do this
randomline = random.choice(lines)
chthonicdaemon
  • 19,180
  • 2
  • 52
  • 66
0

This is for integer random that is shown testfile.txt

import random
file = open("testfile.txt","w")
file.write(str(random.randint(3,115)))
file.close()
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Alphard
  • 25
  • 3