lundi 31 janvier 2022

How to choose a one or two-parted word(name) from file without "|" separator in python

Hello i'm looking for solution to solve my problem:

I have text file countries-and-capitals.txt with example words:

Poland | Warsaw
Sri Lanka | Colombo

and i need to write code that will pick a random name of Country or Capital.

My problem is that i can't figure out how to pick in example Sri Lanka without "|" separator and without Colombo word.

I've stuck on

import random

word = random.choice(open("countries-and-capitals.txt").readlines())

but this code picks whole line with "\n".




Aucun commentaire:

Enregistrer un commentaire