Reputation: 313
I'm messing around with numpy trying to create a 3x3 matrix. I want to capture the matrix input via user input and then print the matrix out as the user entered it.
Here's what I have now, it throws a
ValueError: invalid literal for int() with base 10:
when I run it and I have no idea why. I'm not entering the letter "a" anywhere, only numbers.
def matrix():
row = int(3)
column = int(3)
matrix_input = []
print("Enter the entries in a single line (separated by space): ")
for i in range(row): # A for loop for row entries
entries = []
for j in range(column): # A for loop for column entries
entries.append(int(input()))
matrix_input.append(entries)
# matrix_input = np.array(entries).reshape(row, column)
print(matrix_input)
The goal is to get the user to input 3 numbers on three separate lines. Example:
130
304
603
The program would then type this back out exactly as the user entered it and in the same format.
130
304
603
Any guidance would be much appreciated. Thanks
Upvotes: 0
Views: 1097
Reputation: 313
This does the trick.
def matrix():
row = int(3)
column = int(3)
matrix_input = []
print("Enter the entries in a single line (separated by space): ")
for i in range(row): # A for loop for row entries
# ints = input().split()
while True:
ints = input().split()
if len(ints) == 3:
break
print("Invalid Input Received")
print("Enter the entries in a single line (separated by space): ")
entries = []
for a in ints:
entries.append(int(a))
matrix_input.append(entries)
print("Output")
for ele in matrix_input:
for d in ele:
print(d, end=' ')
print('')
Upvotes: 0
Reputation: 2782
def matrix():
row = int(3)
column = int(3)
matrix_input = []
print("Enter the entries in a single line (separated by space): ")
for i in range(row): # A for loop for row entries
ints =input()
entries = []
for a in ints:
entries.append(int(a))
matrix_input.append(entries)
for ele in matrix_input:
for d in ele:
print(d,end='')
print('')
matrix()
Input:
Enter the entries in a single line (separated by space):
130
304
603
Output:
130
304
603
Upvotes: 1
Reputation: 246
Right now, your code asks for each element seperately. If you enter numbers until it ends itself like
111
222
333
444
555
666
777
888
999
the program will return
[[111, 222, 333], [444, 555, 666], [777, 888, 999]]
This happens because the input()
is inside the inner most loop, and is consequently called 9 times.
So if you want to enter the values separated by space as indicated, you could use list comprehension to convert this to a list of numbers:
row = int(3)
matrix_input = []
print("Enter the entries in a single line (separated by space): ")
for i in range(row): # A for loop for row entries
matrix_input.append([int(k) for k in input().split(' ')])
for out in matrix_input:
print('{0} {1} {2}'.format(*out))
This will ask for three entries, which are split along the spaces and converted to integer. If you want floats, you could use float(k)
instead of int(k)
.
The print command uses unpacking and the fact that the for ... in
returns the rows.
Alternatively, you could use ' '.join()
, which is more flexible:
for out in matrix_input:
print(' '.join([str(el) for el in out]))
This is in fact the inverse operation of the construction of the matrix.
Upvotes: 3
Reputation: 698
The problem is that you're trying to convert an entire string (the input with spaces between each int) to an integer.
Instead you need to split out the input. See below:
def matrix():
row = int(3)
column = int(3)
matrix_input = []
print("Enter the entries in a single line (separated by space): ")
input_str = input()
entries = []
entries.extend([int(x) for x in input_str.split(' ')])
for item in entries:
print(item)
Upvotes: 0