Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to translate the texts with python?

I’m trying to find a way to translate some texts with else if but I can’t find the way to do it:
What I try to translate is the following part of the code:

rarity = {api["rarity"]}
        if rarity:
            rarity="epico"
        else:
            rarity="epic"



            if rarity:
                rarity="un poco comun"
            else:
                rarity="uncommon"   



        
        print(f'Rareza: {rarity}')

But I can’t figure out how to do it

This is the complete code that I am using:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import json
import requests

args= input("Escribe: ")

response = requests.get("https://jose89fcb.es/apifortnite/fish.php")
data = response.json()


match_found = False
for api in data['fish']:
    
    if args == api["name"]: 
        print(f'Imagen: {api["image"]}')
        print(f'Descripción: {api["description"]}')



        rarity = {api["rarity"]}
        if rarity:
            rarity="epico"
        else:
            rarity="epic"



            if rarity:
                rarity="un poco comun"
            else:
                rarity="uncommon"   



        
        print(f'Rareza: {rarity}')


        match_found = True
if not match_found:
    print("Error")

Could someone give me a hand? Thank you very much in advance!

>Solution :

I don’t think you understand what if rarity means. That just means, check if its value is considered True or False. If a string has any content in it, this will be considered True. Perhaps you meant to check if its content was equal to the English version, and you want to translate it to the Spanish version?

You could do this via if-elif:

rarity = {api["rarity"]}
if rarity == "epic":
    rarity = "epico"
elif rarity == "uncommon":
    rarity = "un poco comun"

print(f'Rareza: {rarity}')

Or a dict:

translations = {
    "epic": "epico",
    "uncommon": "un poco comun"
}

rarity = translations[rarity]
print(f'Rareza: {rarity}')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading