Home > database >  No output from my function after using return and calling the function in Python
No output from my function after using return and calling the function in Python

Time:08-16

After some help I managed to fix up my previous code. This code counts and stores the number of cities with certain continent codes from 3 sets of arrays. However, I still don't get any output from my function when I call it and I was wondering why. I tried unindenting the print function, but because it isn't a global variable it doesn't work.

def countCities(c1, c2, c3):
  cities = {"eu": 0, 
  "as": 0,
  "am": 0,
  "sa": 0,
  "af": 0}
  #creating a dictionary for the cities and their values
  for x in c1   c2   c3:
    code = x.split("-")[-1]
    #assigning the later part of the input(e.g the -eu part)to code
    if code in cities:
      cities[code]  = 1
  return cities
  print(cities)
  #what I thought would have given an output?
cities1 = ["london-eu","bangkok-as", "madrid-eu"]

cities2 = ["paris-eu","milan-eu", "madrid-eu", "budapest-eu"]

cities3 = ["houston-am","milan-eu", "bogota-sa", "nairobi-af"]

countCities(cities1, cities2, cities3)
#calling the function

CodePudding user response:

This works fine for me?

def countCities(c1, c2, c3):
  cities = {"eu": 0, 
  "as": 0,
  "am": 0,
  "sa": 0,
  "af": 0}
  #creating a dictionary for the cities and their values
  for x in c1   c2   c3:
    code = x.split("-")[-1]
    #assigning the later part of the input(e.g the -eu part)to code
    if code in cities:
      cities[code]  = 1
  return cities
 
cities1 = ["london-eu","bangkok-as", "madrid-eu"]

cities2 = ["paris-eu","milan-eu", "madrid-eu", "budapest-eu"]

cities3 = ["houston-am","milan-eu", "bogota-sa", "nairobi-af"]

counts = countCities(cities1, cities2, cities3)

print(counts)

Another approach

I tried another way as well, doing the same as your code, but perhaps more general in that you just append a new list of cities into the cities array:

from collections import Counter

cities = [["london-eu","bangkok-as", "madrid-eu"],
          ["paris-eu","milan-eu", "madrid-eu", "budapest-eu"],
          ["houston-am","milan-eu", "bogota-sa", "nairobi-af"]]

cities = sum(cities, [])

cnt = Counter(city.split("-")[1] for city in cities)

print(dict(cnt))

CodePudding user response:

I reformatted your code and ran this on my machine, it could just be a formating issue because it seems to have worked fine on my end:

def countCities(c1, c2, c3):
    cities = {"eu": 0, 
    "as": 0,
    "am": 0,
    "sa": 0,
    "af": 0}
    #creating a dictionary for the cities and their values
    for x in c1   c2   c3:
        code = x.split("-")[-1]
        #assigning the later part of the input(e.g the -eu part)to code
        if code in cities:
            cities[code]  = 1
    return cities

  #what I thought would given an output?
cities1 = ["london-eu","bangkok-as", "madrid-eu"]

cities2 = ["paris-eu","milan-eu", "madrid-eu", "budapest-eu"]

cities3 = ["houston-am","milan-eu", "bogota-sa", "nairobi-af"]

print(countCities(cities1, cities2, cities3))
  • Related