Help in fixing this code


Joined
May 8, 2022
Messages
4
Reaction score
0
Hello all first of all, nice forum.
I just joined today! In hopes to find some help to my coding errors, because I just started to learn python and I like it so far.
So I'm going directly to the matter. I'll first post what's the code expected to do and then what I have done so far.
Note the code worked fine but wasnt fully functional and now it seems a bit more broken as I tried to polish it against wrong typing from a potential user.

This code pretends to first fetch a number of students or a number of califications to be more precise, then proceeds to ask for the calification grade each time until reaching the number first specified in the code, and also does catch the lowest and highest grade of those entered, it also makes an average of the grades.

Sorry for the broken language, I'm not native. Please have some patience with me.

I have something like this:

Python:
def read_int():
    while True:
        try:
            x = 0
            sum_grades = 0
            lowest_grade = 7
            highest_grade = 1
            students = int(input("Enter the number of students to evaluate: "))
            counter = 0
            while x < students :
                try:
                    counter = counter + 1
                    students = students - 1
                    grade = float(input("Enter the grade of the student: "))
                    x = x + 1
                    if grade < 1 or grade  > 7:
                        print("Please, enter a valid grade...")
                        break
                    else:
                        while True:
                            try:
                                sum_grades = sum_grades + grade
                                if lowest_grade >= grade  :
                                    lowest_grade = grade
                                if highest_grade <= grade  :
                                    highest_grade = grade
                                average = round(sum_grades / counter, 1)
                                print("The average is ", average )
                                print("Lowest grade ", lowest_grade )
                                print("highest grade ", highest_grade )
                                break
                            except ValueError:
                                print("Please, enter a numeral...")
                                print("")
                except ValueError:
                    print("Please, enter a numeral...")
                    print("")
        except ValueError:
            print("Please, enter a numeral...")
            print("")
read_int()
 
Ad

Advertisements

Joined
May 11, 2022
Messages
28
Reaction score
0
Python:
def read_int():
    while True:
        try:
            x = 0
            sum_grades = 0
            lowest_grade = 7
            highest_grade = 1
            students = int(input("Enter the number of students to evaluate: "))
        except ValueError:
            print("The number of students must be an integer")
            continue
        counter = 0
        grade = 0.0
        while x < students :
            while True:
                try:
                    grade = float(input("Enter the grade of the student: "))
                except ValueError:
                    print("only integers and floats")
                    continue
                if grade < 1.0 or grade > 7.0:
                    print("only values between 1 and 7")
                    continue
                break                 
            counter = counter + 1
            x = x + 1
            sum_grades = sum_grades + grade
            if lowest_grade >= grade  :
                lowest_grade = grade
            if highest_grade <= grade  :
                highest_grade = grade
            average = round(sum_grades / counter, 1)
            print("The average is ", average )
            print("Lowest grade ", lowest_grade )
            print("highest grade ", highest_grade )
        check =input("would you like to grade more students? Y/N ")
        if check == "Y":
           pass
        else:
           break
here's my fixes.
 
Joined
May 8, 2022
Messages
4
Reaction score
0
Thanks for the reply but when run the code gives me:
Process finished with exit code 0
 
Joined
May 8, 2022
Messages
4
Reaction score
0
So apparently I made it. Many thanks to phillip1882 for his will to help me anyway.
This code asks for input to get how many grades are to be reviewed, after which proceeds to gather such grades one by one, string error proof, and within a valid range for grades from 1 to 7 (califications system for native country), to then calculate the highest and lowest grade from the entered lot, aswell as the average of all grades for that round, to end asking either for further input to keep on reviewing grades or if the user wants to finish altogether.
Please note the language is not English, by the way.
Python:
def read_float():
    x = 0
    while x == 0:
        try:
            lista = []
            cantidad = int(input("Ingrese la cantidad de notas a evaluar: "))
            contador = 0
            while cantidad != contador:
                try:
                    contador += 1
                    notas = float(input(f"Ingrese nota #{contador}:"))
                    while notas > 7 or notas < 1:
                        contador -= 1
                        print("Por favor, ingrese notas válidas.")
                        break
                    else:
                        if 1 <= notas <= 7:
                            lista.append(notas)
                except ValueError:
                    print("Por favor, ingrese un valor numérico...")
                    print("")
                    contador -= 1
                    continue
            print(f"Las notas ingresadas secuencialmente fueron: {lista}")
            print("La nota más alta es:", max(lista))
            print("La nota más baja es:", min(lista))
            suma = sum(lista)
            promedio = round(suma / len(lista), 1)
            print(f"El promedio es: {promedio}")
            x += 1
            while x == 1:
                try:
                    seguir = int(input("\n"
                                       "¿Desea continuar evaluando notas?\n"
                                       "1.- Sí.\n"
                                       "2.- No.\n"
                                       ":"))
                    if seguir == 2:
                        print("Terminado...\n"
                              "")
                        x += 1
                        break
                    elif seguir == 1:
                        x -= 1
                        print()
                        pass
                    else:
                        if seguir != 1 or seguir != 2:
                            print("Por favor, ingrese una opción válida.")
                except ValueError:
                    print("Por favor, ingrese una opción válida.")
                    print("")
        except ValueError:
            print("Por favor, ingrese un valor entero.")
            print("")


read_float()
 
Last edited:
Ad

Advertisements

Joined
May 11, 2022
Messages
28
Reaction score
0
your second code post is in much the same manner as your first. go over my code answer and copy as much as you can.
in particular your try except blocks should be handled better.
 
Joined
May 8, 2022
Messages
4
Reaction score
0
Well it works but not as I was expected it to be, for example, after the first prompt is passed, every time a grade is put in, it instantly do the calculations and shows the average, highest and lowest grades, I did a run and choose to input 4 grades and it stopped to the second one after trying the other values, by entering numbers out of range or by sending blank input, also when asked if you want to continue reviewing grades, you can just press any input and it will finish anyhow, so it's not fully error proof against typos that might stop your code fully. But thanks for your good will, I hope you can help me in the future If I'm lost to find a solution for a code project, so your help will always be appreciated.
 
Ad

Advertisements

Joined
May 11, 2022
Messages
28
Reaction score
0
I'm debating whether or not to fix the second code post. you didn't seem to learn anything with my first correction.
I'll leave it up to you. If you want me to fix again, I will, if you want to try on your own, I would appreciate it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top