Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Ground Controller Lvl 1
Message 1 of 2

Hello i need help in doing the read to text file in python

Solved! Go to Solution.

Essentially i am buliding a card game but i dont know how to read to text file.As well that i need to assign the names with random values .If you could point me to an external website to learn i would be very greatful.

1 Reply
Solution
Commander Lvl 1
Message 2 of 2

Re: Hello i need help in doing the read to text file in python

Hi @NeoThe-One,

 

Welcome to the Github Community Forum!

 

I can try to solve your problem with some example.

 

Read text files with Python3.x

with open("<name_of_file>.<extension>", "r") as file_name:
                lines = file_name.readlines()
               # now lines represents a list of all lines inside file_name
               # you can parse this lines with something like this
               for line in lines:
                     # do something

 

Assign to variable random values

import random

value_int = random.randint(0,100)

name_list = ["name1","name2","name3"]
value_name = name_list[random.randint(0,len(name_list)-1)]

 

-Gabriele-

 


Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!