1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Mar 13 14:25:56 2018 @author: beast Note this program is for holding int value to list only """ def inmatrix(): matrixA=list() # intialize matrix as empty list try: row,col=[int(j) for j in (input("Enter the row and col ").split())] print(row,col) inp="" except ValueError: print("Please Enter row and col seperated by space eg: 2 2") inmatrix() for i in range(row): for j in range(col): inp+=((input("Enter the number--->"))+",") try: lst=[int(a) for a in inp.split(",") if a!=""]#delete int for making it to hold any data # create a list using input row string matrixA.append(lst) #add the row wise data inp="" #after eacch row input reset inp to get next input except ValueError: print("please Enter Integer Only as list input\n\a------ Restarting ---\n") inmatrix() return matrixA # print(matrixA) if __name__=="__main__": try: print(inmatrix()) except Exception: print("please Give Correct Input\n") inmatrix() |
this below code is alternate version for accepting anything(character as nested list input )..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Mar 13 14:25:56 2018 @author: beast Note this program is for holding int value to list only """ def inmatrix(): matrixA=list() # intialize matrix as empty list try: row,col=[int(j) for j in (input("Enter the row and col ").split())] print(row,col) inp="" except ValueError: print("Please Enter row and col seperated by space eg: 2 2") inmatrix() for i in range(row): for j in range(col): inp+=((input("Enter the number--->"))+",") try: lst=[a for a in inp.split(",") if a!=""]#delete int for making it to hold any data # create a list using input row string matrixA.append(lst) #add the row wise data inp="" #after eacch row input reset inp to get next input except Exception: print("\n--AN ERROR occured while taking input to list,please try again\n") inmatrix() return matrixA # print(matrixA) if __name__=="__main__": try: print(inmatrix()) except Exception: print("please Give Correct Input\n") inmatrix() |
Comments
Post a Comment
share your thoughts ....