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
  • 69,019
  • 8
  • 93
  • 175
Santhosh
  • 851
  • 3
  • 12
  • 30

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
  • 302,178
  • 90
  • 548
  • 690
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:

****
*******
*****
mailtosumitrai
  • 691
  • 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,147
  • 1
  • 13
  • 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
  • 10