python 2.7 - Taking an input as multiple elements instead of only 1 -
i'm trying create function take input of user create 3x4 matrix, find sum of each column individually. i'm not sure how set input i'll given each number individually instead of long string.
def testmatrixfunctions(): row0 = input("enter 3-by-4 matrix row row 0: ") row1 = input("enter 3-by-4 matrix row row 1: ") row2 = input("enter 3-by-4 matrix row row 2: ") i use adding columns, biggest concern input @ moment.
use str.split() method:
>>> "1 2 3 4 5".split() ['1', '2', '3', '4', '5'] and convert each string integer:
>>> map(int, "1 2 3 4 5".split()) [1, 2, 3, 4, 5] >>> [int(c) c in "1 2 3 4 5".split()] [1, 2, 3, 4, 5]
Comments
Post a Comment