IP addresses are quite commonly used for geo-targeting i.e. customizing the content of a website by the visitor's location / country but they are not permanently associated with a country and often get re-assigned.
To accomplish what you want, you need to keep an up to date lookup to map an IP address to a country either with a database or a geolocation API. Here's an example :
> https://ipapi.co/8.8.8.8/country
US
> https://ipapi.co/8.8.8.8/country_name
United States
Or you can use the full API to get complete location for IP address e.g.
https://ipapi.co/8.8.8.8/json
{
"ip": "8.8.8.8",
"city": "Mountain View",
"region": "California",
"region_code": "CA",
"country": "US",
"country_name": "United States",
"continent_code": "NA",
"postal": "94035",
"latitude": 37.386,
"longitude": -122.0838,
"timezone": "America/Los_Angeles",
"utc_offset": "-0800",
"country_calling_code": "+1",
"currency": "USD",
"languages": "en-US,es-US,haw,fr",
"asn": "AS15169",
"org": "Google Inc."
}