1

I have an assignment where I'm handed an array with numbers, integers, floats and possibly strings. I then have to identify which of the elements are contained in another array with pure integers and which are not. Those which are not contained in the array with integers must be printed and the user must change the element to a value that is contained in the array with integers. Though I have the problem that if the element in the element in the given array is a float, the output from the user's input also becomes a float, (Unless the input is a value from the array with integers.) The same problem also occurs if the element in the given array is an integer and the user's input is a float. Then the float rounds down to an integer. Can anyone give any tips how I should change this code, so the script runs flawless?

grades = np.array([-3,-10.5,0,7,4,8,4,7,10,5])
SevenGradeScale = np.array([-3, 0, 2, 4, 7, 10, 12])
SevenGradeScale = SevenGradeScale.astype(int)
for i in range(np.size(grades)):
    if grades[i] not in SevenGradeScale:
        while True:  
            if grades[i] in SevenGradeScale:
                grades = grades.astype(int)
                print("The grade has been changed to {:d}. ".format(grades[i]))
                break
            elif type(grades[i]) is np.float64:
                print("\n{:.1f} is not a valid grade. The grade must be an integer.".format(grades[i]))
            elif type(grades[i]) is np.int32:
                print("\n{:d} is not within the seven grade scale.".format(grades[i]))
            elif type(grades[i]) is str:
                type("\n{:s} is not a valid grade.".format(grades[i]))
            try:
                grades[i] = float(input("Insert new grade: "))
            except ValueError:
                pass

You would probably comment the "float(input())" but this somehow helped my script. Though I don't know if there are other possibilities.

When running the code and typing random inputs, I get following results

-

10.5 is not a valid grade. The grade must be an integer.

Insert new grade: 10.7

10.7 is not a valid grade. The grade must be an integer.

Insert new grade: 10
The grade has been changed to 10. 

8 is not within the seven grade scale.

Insert new grade: 7.5
The grade has been changed to 7. 

5 is not within the seven grade scale.

Insert new grade: 5.5

5 is not within the seven grade scale.

Insert new grade: string

5 is not within the seven grade scale.

Insert new grade: 0
The grade has been changed to 0.
  • Why are you using numpy? numpy arrays contain only one type, that you have declared as integer here. – Reblochon Masque Jan 17 '18 at 11:43
  • just try like take the input from respective if parts and pass it to `garde[i]` – Vikas Periyadath Jan 17 '18 at 11:50
  • Isn't this a question about [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Mr. T Jan 17 '18 at 12:00

0 Answers0