-1

I'm trying to print out two statements on the basis of provided dictionary, but something goes wrong and the code is stopping after the first f-string (the second isn't displayed). I suppose that it might be something similar, but can't figure out the mistake:

data = [
{
    'name': 'Instagram',
    'follower_count': 346,
    'description': 'Social media platform',
    'country': 'United States'
},
{
    'name': 'Cristiano Ronaldo',
    'follower_count': 215,
    'description': 'Footballer',
    'country': 'Portugal'
}]

a = input(f"Compare A: {data[1]['name']}, {data[1]['description']}, from {data[1]['country']}")
b = input(f"Compare B: {data[0]['name']}, {data[0]['description']}, from {data[0]['country']}")

2 Answers2

4

How about this?

data = [
{
    'name': 'Instagram',
    'follower_count': 346,
    'description': 'Social media platform',
    'country': 'United States'
},
{
    'name': 'Cristiano Ronaldo',
    'follower_count': 215,
    'description': 'Footballer',
    'country': 'Portugal'
}]

a = print(f"Compare A: {data[1]['name']}, {data[1]['description']}, from {data[1]['country']}")
b = print(f"Compare B: {data[0]['name']}, {data[0]['description']}, from {data[0]['country']}")

Here is the result:

Compare A: Cristiano Ronaldo, Footballer, from Portugal
Compare B: Instagram, Social media platform, from United States
QQQ
  • 327
  • 1
  • 6
0

This is an example on how to print a and b:

data = [
{
    'name': 'Instagram',
    'follower_count': 346,
    'description': 'Social media platform',
    'country': 'United States'
},
{
    'name': 'Cristiano Ronaldo',
    'follower_count': 215,
    'description': 'Footballer',
    'country': 'Portugal'
}]

print("Compare A: {" + data[1]['name'] + "}, {" + data[1]['description'] + "}, " + "from {" + data[1]['country']"}") print("Compare B: {" + data[0]['name']+ "}, {" + data[0]['description'] + "}," + "from {" + data[0]['country'] + "}")

Anna Maule
  • 268
  • 1
  • 9