2

I wrote a simple map() function that iterates over a list and prints as many '*' that are in the list. I see a small problem with my code, I see an extra 'None' in my output. Could someone help me debug this issue?

Problem Statement:
-----------------
Define a procedure histogram() that takes a list of integers and prints a
histogram to the screen. For example, histogram([4, 9, 7]) should print the
following:

****
*********
*******

Source Code

def print_asterisks(num):
    print ''.join('*' for i in xrange(num))

def histogram(s):
    map(print_asterisks, s)

def main():
    # Test inputs
    print histogram([4,7,5])


if __name__ == "__main__":
    import sys
    sys.exit(main())

Output

****
*******
*****
None
Will Ness
  • 70,110
  • 9
  • 98
  • 181
Santhosh
  • 891
  • 3
  • 12
  • 31

4 Answers4

4

In this line:

print histogram([4,7,5])

You are printing the return value of the histogram function, which is None. You only need to call the function:

histogram([4,7,5])
wim
  • 338,267
  • 99
  • 616
  • 750
1

Since the function histogram isn't returning anything. print histogram([4,7,5]) prints None.

def print_asterisks(num):
    print ''.join('*' for i in xrange(num))

def histogram(s):
    map(print_asterisks, s)

def main():
    # Test inputs
    histogram([4,7,5])


if __name__ == "__main__":
    import sys
    sys.exit(main())

Output:

****
*******
*****
sumit-sampang-rai
  • 701
  • 1
  • 7
  • 16
1

Replace

def main():
    # Test inputs
    print histogram([4,7,5])

with

def main():
    # Test inputs
    histogram([4,7,5])

Note: the function histogram doesn't have any return statement.

kvivek
  • 3,321
  • 1
  • 15
  • 17
1

write histogram([4,7,5]) instead print histogram([4,7,5])


def print_asterisks(num):
    print ''.join('*' for i in xrange(num))

def histogram(s):
    map(print_asterisks, s)

def main():
    # Test inputs
    histogram([4,7,5])

if __name__ == "__main__":
    import sys
    sys.exit(main())
abhijeetmote
  • 131
  • 1
  • 10