0

Hi I'm new to Python and have been learning independently. However, although seemingly simple I can't get past this problem.

So I'm creating a very simple chart whereby I enter a list of numbers and get corresponding pipes (|) to match.

def chart(list):
    for i in list:
        print('|' * i)
chart([2,7,1,4,2,3,9,3])

Gives output:

 ||
 |||||||
 |
 ||||
 ||
 |||
 |||||||||
 |||

However how would I do it so that the entered data "chart([2,7,1,4,2,3,9,3])" would not be done from the developed code itself? I tried:

def chart(list):
    for i in list:
        print('|' * i)
a = list(input("Enter List of nums: "))
chart([a]

But that error-ed? Any Solutions? Thanks for the help! (Ignore any indentation errors just typed it up without Python)

  • if you instruct the user to enter number separated by spaces, then you can call `chart(a.split())` on the input but you would need to typecast i to `int(i)`) – Woody Pride Oct 17 '17 at 09:39
  • Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – Chris_Rands Oct 17 '17 at 09:39
  • @Chris_Rands hmmm not sure? Had a look at it but still didn't really understand an explanation to why mine doesn't work? – TheWickedest Oct 17 '17 at 09:46
  • @TheWickedest `input()` returns a string in Py 3, you want a list so if the user inputs `'1,2,3,4'` then use `a = [int(i) for i in input().split(',')]` (in python 2 use `raw_input()` instead of `input()`) – Chris_Rands Oct 17 '17 at 09:48
  • Ahh got it working @Chris_Rands Thanks! – TheWickedest Oct 17 '17 at 09:54
  • `print '\n'.join(['|'*n for n in map( int , raw_input().strip().split() ) ])` . Enjoy learning python :) . – Abhimanyu singh Oct 17 '17 at 09:57

1 Answers1

0

How about using:

my_input = input("Enter List of nums: ")
input_list = [int(e) for e in my_input.split()]

The input looks like:

Enter List of nums: 1 2 3 4

and then pass the input_list to your function