lakshmen
lakshmen

Reputation: 29084

User input for triangles in python

I would like to create a triangle and take user input from the user. I have already created the function for creating triangles.

Function:

def triangle(rows):
    PrintingList = list()
    for rownum in range (rows ):     
        PrintingList.append([])
        for iteration in range (rownum):
            newValue = raw_input()
            PrintingList[rownum].append(newValue)

But this takes input in this way..

3 
7
4
2 
4 
6
8 
5 
9 
3

I need it to take a input like this:

3 
7 4
2 4 6
8 5 9 3

How do it change it to take input in this way? need some guidance on this...

Upvotes: 0

Views: 245

Answers (1)

Oleksii Kachaiev
Oleksii Kachaiev

Reputation: 6234

for rownum in range (rows ):     
    PrintingList.append([])
    newValues = raw_input().strip().split()
    PrintingList[rownum] += newValues

I don't see here if you need or not to convert input from strings to ints.. But if you need, this will look like

for rownum in range (rows ):     
    PrintingList.append([])
    newValues = map(int, raw_input().strip().split())
    PrintingList[rownum] += newValues

Upvotes: 4

Related Questions