Home > Back-end >  Extra element in def function
Extra element in def function

Time:10-22

So, that`s my code:

def city_country(f_city, f_country, s_city, s_country, t_city, t_country):
    print(f"{f_city.title()}, {f_country.title()}")
    print(f"{s_city.title()}, {s_country.upper()}")
    print(f"{t_city.title()}, {t_country.title()}")
    if f_country == 'usa' or f_country == 'Usa':
        print(f"{f_country.upper()}")
    elif s_country == 'usa' or s_country == 'Usa':
        print(f"{s_country.upper()}")
    elif t_country == 'usa' or t_country == 'Usa':
        print(f"{t_country.upper()}")
    return 
city_country('moscow', 'russia', 'washington', 'usa', 'berlin', 'germany')

and everything would be fine, but for some reason I have an extra element displayed at the end in the output of the function:

Moscow, Russia
Washington, USA
Berlin, Germany
USA

Any assumptions?

CodePudding user response:

It is because of following comparison

elif s_country == 'usa' or s_country == 'Usa':
      print(f"{s_country.upper()}")

And from input you have passed usa to s_country variable

CodePudding user response:

I agree with @John Byro

If you want your code to output:

Moscow, Russia

Washington, USA

Berlin, Germany

Then you should use:

def city_country(f_city, f_country, s_city, s_country, t_city, t_country):
    print(f"{f_city.title()}, {f_country.title()}")
    print(f"{s_city.title()}, {s_country.upper()}")
    print(f"{t_city.title()}, {t_country.title()}")
    return 

#Method Call
city_country('moscow', 'russia', 'washington', 'usa', 'berlin', 'germany')

You can see the output here

  • Related