-1

I am making a single player guessing game on capital cities, where the country and the first letter of the capital city are displayed and the user has 2 guesses to input the correct answer.

In an external .txt, I have the countries and cities stored, where they are separated by a '-'. Here is an example of how they are formatted:

England - London

France - Paris

India - New Dehli

Say the 2nd line was randomly selected, I want the program to output this:

"The country is France and the first letter of the capital city is P. Try and guess!"

Any help would be appreciated! :)

rose
  • 19
  • 2
  • 1
    Does this answer your question? [Split a string by a delimiter in python](https://stackoverflow.com/questions/3475251/split-a-string-by-a-delimiter-in-python) – mkrieger1 Oct 27 '20 at 20:26
  • No, not exactly. I only want the word before the dash to be printed. Thank you for the comment though! – rose Oct 27 '20 at 20:28
  • Well, once you split the string, do you know how to index a list? – OneCricketeer Oct 27 '20 at 20:31
  • No, how would you do that? – rose Oct 27 '20 at 20:32
  • Well. In order to do that, you need to split the string. That’s what the other question is about. Or did you already know how to split the string at the dash? Then you need to ask a more specific question. – mkrieger1 Oct 27 '20 at 20:32
  • I know how to split the string, I just don't know how to print the word before the dash. – rose Oct 27 '20 at 20:34
  • 2
    So if you were given `[1,2,3,4]` and asked to get `1`, you couldnt do it in Python? Splitting a string, and access a word works the same way – OneCricketeer Oct 27 '20 at 20:34
  • I'm not sure how you would access the word. How do you do this? – rose Oct 27 '20 at 20:43
  • Can you show the code you have written so far, where you split the string into two words and now want to print the first word? – mkrieger1 Oct 27 '20 at 20:43
  • import random line= random.choice(open("countries.txt").readlines()) country= line.split(" - ") print (country) **sorry I don't know how to send this properly. I am very new at stack overflow! – rose Oct 27 '20 at 20:44
  • 1
    Did you already read this? https://docs.python.org/3/tutorial/introduction.html#lists – mkrieger1 Oct 27 '20 at 20:46
  • Yes, thank you for the link. So, would I write: print[0] at the end? – rose Oct 27 '20 at 20:50
  • `print[0]` would be wrong, as you can discover when trying it. – mkrieger1 Oct 27 '20 at 20:56

4 Answers4

0

so random text is the chosen text. You then split it by ' - ' and that creates a list like this ['France', 'Paris'] so then using the 'f' string you put in those variable but remember that they are a list so to access the country you need to access list index 0 which is first item in list like this random_text[0] for the first letter in word 'Paris' you first access the word like this random_text[1] which is the second item in the list and then you access that items' first character like this random_text[1][0] and print it out

random_text = 'France - Paris'
random_text = random_text.split(' - ')
print(f'The country is {random_text[0]} and the first letter of the capital city is {random_text[1][0]}. Try and guess!')
Matiiss
  • 5,203
  • 2
  • 11
  • 27
0

Because you know what's your data looks like (XXX - YYY), a simple split by dash and white spaces will do:

selected = "England - London"
country, city = selected.split(" - ")
print(f"The country is {country} and the city is {city}")
Dharman
  • 26,923
  • 21
  • 73
  • 125
OmerM25
  • 251
  • 3
  • 13
0

Use split.(), it's really useful in sperating strings. The code:

city = ["England - London","France - Paris","India - New Dehli"]
random_guess = city.split(' - ')

If you want to print random country and it's capital city, you'll need to import random

import random
city = ["England - London","France - Paris","India - New Dehli"]
random_guess = random.choice(city).split(' - ')
print("The country is",random_guess[0],"and the first letter of the capital city 
is",random_guess[1][0])

You can later add more element into the city list.

Dav_Did
  • 190
  • 10
0

This is my solution.

import re

exp = re.compile(r"(?P<country>\w+)[ ]*-[ ]*(?P<city>\w+)")


def __test(text):
    match = exp.match(text)
    print("The country is {country} and the city is {city}".format(**match.groupdict()))
    
    
__test("England - London")
max wiklund
  • 103
  • 1
  • 5