0

I am beginner in python. This is a code I had written in C. I am trying to convert it to python.

int arr[studentCount][subjectCount][2];
const int DISTINCTION = 4;
const int FIRST_DIVISION = 3;
const int SECOND_DIVISION = 2;
const int PASS_CLASS = 1;
const int FAIL = 0;

for(int i=0 ; i<studentCount ; i++) {
    for(int j=0 ; j<subjectCount ; j++) {
        //taking input in i,j,0
        printf("Enter marks of student %d subject %d - ",i+1,j+1);
        scanf("%d",&arr[i][j][0]);

        //logic of result
        //giving grades in i,j,1 based on the marks entered in i,j,0
        if(arr[i][j][0]>=70)
            arr[i][j][1]=DISTINCTION;
        else if(arr[i][j][0]>=60)
            arr[i][j][1]=FIRST_DIVISION;
        else if(arr[i][j][0]>=50)
            arr[i][j][1]=SECOND_DIVISION;
        else if(arr[i][j][0]>=40)
            arr[i][j][1]=PASS_CLASS;
        else
            arr[i][j][1]=FAIL;

    }
}

This is where I reached but can't under stand how do I take input in the 3d array

arr = np.zeros((studentCount,subjectCount,2));
print arr;
for (i in range(studentCount)):
    for(j in range(subjectCount)):
        #taking input in the arr

In the code I am basically taking input of marks of studentCount students in subjectCount subjects and then assigning them grades according to the marks. So here I don't want to take the input in a line and all the previous answers I searched show the inputs taken in a line. If I do that I will be taking the input of grades by user and hence defeating the purpose of the code. Please Help!

petezurich
  • 9,280
  • 9
  • 43
  • 57
Nischaya Sharma
  • 431
  • 2
  • 6
  • 16
  • To get `i` and `j` you'll want to look at python `enumerate` https://docs.python.org/2/library/functions.html#enumerate and you'll want to replace the `printf`/`scanf` with `input` https://docs.python.org/2/library/functions.html#input – Red Cricket Nov 24 '18 at 09:29
  • Thanks @RedCricket for the enumerate guide. – Nischaya Sharma Nov 24 '18 at 09:35
  • Usually you supply a prompt as an argument to `input()` as in `something = input('Enter something')` if you are expecting an integer you'll have to cast it like `age = int(input('How old are you? '))` – Red Cricket Nov 24 '18 at 09:38

1 Answers1

1

You want to use Python input(). You could do it in your code something like this:

import numpy as np

arr = np.zeros((studentCount,subjectCount,2))
print arr
for i in range(studentCount):
    for j in range(subjectCount):
        arr[i][j][0] = input("Enter marks of student %d subject %d:" % (i, j))
Geeocode
  • 5,705
  • 3
  • 20
  • 34
  • @NischayaSharma I'm happy, if it helped, please accept my answer :). – Geeocode Nov 24 '18 at 09:48
  • Also why were they appending in https://stackoverflow.com/questions/22741030/how-to-input-matrix-2d-list-in-python-3-4. It confused me. – Nischaya Sharma Nov 24 '18 at 09:51
  • @NischayaSharma It is about base matrices, called list https://docs.python.org/2/tutorial/datastructures.html in python, not about advanced matrices of numpy array https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.array.html. – Geeocode Nov 24 '18 at 09:57
  • @NischayaSharma Your welcome again and please **accept my answer** again. Below the "0" and up down arrows. – Geeocode Nov 24 '18 at 10:06