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:
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}')