I can't solve all the cases | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I can't solve all the cases

I am doing the first project of the "Data Science" course called "Average of Rows". I make the code to solve the first case with what I have been taught during the lessons and it verifies that it is correct. The problem comes when I try to solve the second case. When I change the code, the second case corrects correctly, but then the first case changes to wrong. Can both cases be solved at the same time? My code: n, p = [int(x) for x in input().split()] import numpy as np #Case 1 number = np.array([[1.5, 1], [2, 2.9]]) number_mean = number.mean(axis = 1) print(number_mean) #Case 2: number2 = np.array([[1, 2], [1, 0.5], [1, 0.3]]) number2_mean = number2.mean(axis = 1) print(number2_mean) Thank you.

5th Nov 2022, 10:24 AM
Andrés
3 Answers
+ 5
You need to use the input() statement again to read the data dynamically, and convert the numbers to float. At the end you should remember to round to 2 decimals as instructed by the task.
5th Nov 2022, 11:18 AM
Tibor Santa
Tibor Santa - avatar
+ 1
It is always a bad idea to "hardcode" the test values in the program itself. The input() statement collects these values from the user, and in the automated tests of the Sololearn assignment, the input is provided by the testing procedure. There are also hidden tests where you won't even know the values. So you need to learn how to use input, and how to process its result. input() will always give you a string. If the line contains multiple numbers, you have to use the split() function to chop it to multiple parts (usually separated by whitespace). And to calculate with the numbers, you have to convert them to int or float. In this case the first line contains int values (number of rows and columns) and the rest of the lines are the actual data values. To organize these data points into a list of lists, which can then be passed to a numpy array, you can do this for example, maybe this would be simplest for a beginner to understand: import numpy as np line = input().split() rows = int(line[0]) cols = int(line[1]) data = [] for x in range(rows): line = input().split() linedata = [] for y in range(cols): linedata.append(float(line[y])) data.append(linedata) number = np.array(data) print(number)
6th Nov 2022, 9:22 PM
Tibor Santa
Tibor Santa - avatar
0
I'm sorry, but I don't know how I should use input() in the exercises. Could you help me? I don't know much about programming.
6th Nov 2022, 10:52 AM
Andrés