0

I have the code which populates the list "oneStudent" with 5 values, I need to validate indexes 1-4 (so the last 4 values) as an integer. I have tried using try...except, however, no matter how I try to implement it, it breaks the code

I'm relatively new to coding and python, it's probably something small I'm missing, any help will be much appreciated

The idea behind the code is to populate the list oneStudent as many times as NumberOfStduent is input above, after each time the oneStudent list will be appended to allStudent and reset for the next one

in the end, allStudent will be a list containing lists with each oneStudent

while True:
    NumberOfStudent = input ("Please input the number of students: ")
    try:
        NumberOfStudent=int(NumberOfStudent)
        if NumberOfStudent in range (1, 6):
            break
        else:
            print("Error, Please input between 1 and 5")
    except:
        print("Error, Please input a valid number")

print("Number of students:", NumberOfStudent)


allStudents=[]
oneStudent=[]
s=0
while s < NumberOfStudent:

        oneStudent.append(input ("Please input student name: ", ))
        oneStudent.append(input ("Please mark for the Web module: ", ))
    try:
        oneStudent[1]=int(oneStudent[1])
        if oneStudent[1] > 0 and oneStudent[1] < 101:
            pass
        else:
            print("error1")
    except:
        print("Error2")

        oneStudent.append(input ("Please mark for the Programming module: ", ))            
    try:
        oneStudent[2]=int(oneStudent[1])
        if oneStudent[2] > 0 and oneStudent[2] < 101:
            pass
        else:
            print("error1")
    except:
        print("Error2")

        oneStudent.append(input ("Please mark for the Graphics module: ", ))

    try:
        oneStudent[3]=int(oneStudent[1])
        if oneStudent[3] > 0 and oneStudent[3] < 101:
            pass
        else:
            print("error1")
    except:
        print("Error2")
     
        oneStudent.append(input("Please mark for the Networking module: ", ))
    try:
        oneStudent[4]=int(oneStudent[1])
        if oneStudent[4] > 0 and oneStudent[4] < 101:
            pass
        else:
            print("error1")
    except:
        print("Error2")

        s = s+1
        allStudents.append(oneStudent)
        oneStudent=[]
        if s == NumberOfStudent:
           break

print(allStudents)
  • *"no matter how I try to implement it, it breaks the code"* - please edit your post to show your attempts and the corresponding error messages. It's hard for us to tell you what went wrong if we can't see the broken code. – 0x5453 Feb 12 '21 at 16:54
  • The very first wrong thing is not defining NumberOfStudent , do that for start. Also your while loop will loop until you reach the number, NumberOfStudent. So your if statement breaking once reaching that number is redundant. – JackTheCrab Feb 12 '21 at 16:58
  • I do apologize, I will edit to show my attempt. also, NumberOfStudent is defined above – Daniel Lovasi Feb 12 '21 at 17:01
  • Number Of Student is not defined, you should define it at the beginning of you code an then you can call it otherwise your while loop will never begin – Joaquín Feb 12 '21 at 17:02
  • You can simply cast the input function (`int(input("Please mark for the ...."))`), it will directly throw an error if the type inputted by the user is not correct : https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers – arhr Feb 12 '21 at 17:07
  • @DanielLovasi, your last edit loops indefinitely as well as asking for just 2 things. Check code by Ankit for what you want. Also the indentation is wrong just before first "try" in "while s" loop. – JackTheCrab Feb 12 '21 at 17:18
  • I have updated the edit, I do apologize for the indentations, it is fine on the code itself but it all shifted when I copied it in – Daniel Lovasi Feb 12 '21 at 17:21
  • arhr, I did try that but the issue with that is I don't want to program to halt if they input a string, for example, I want it to print an error and allow the user to make the input again – Daniel Lovasi Feb 12 '21 at 17:25

2 Answers2

0

This should work:

oneStudent=[]
NumberOfStudent = 2
s=0


def validate_number(text):
    while True:
        try:
            question = int(input(text))
            if 1 <= question <= 100:
                return question 
        except:
            print("That's not a valid option!")
    
    
while s < NumberOfStudent:

        oneStudent.append(input ("Please input student name: ", ))
        web_module = validate_number("Please mark for the Web module: ")
        oneStudent.append(web_module)
        prog_module = validate_number("Please mark for the Programming module: ")
        oneStudent.append(prog_module)
        graph_module = validate_number("Please mark for the Graphics module: ")
        oneStudent.append(graph_module)
        net_module = validate_number("Please mark for the Networking module: ")
        oneStudent.append(net_module)
        s += 1
print(oneStudent)
Joaquín
  • 350
  • 2
  • 12
  • 1
    The if statement is still redundant – JackTheCrab Feb 12 '21 at 17:02
  • indeed, I didn't notice that `if` tbh I'll edit my answer – Joaquín Feb 12 '21 at 17:03
  • Thank you. This solution worked for the issue in question, although I forgot to mention that not only do I have to validate it as an integer but also ensure the number is between 1 and 100 – Daniel Lovasi Feb 12 '21 at 17:18
  • @DanielLovasi let me fix it, one second – Joaquín Feb 12 '21 at 17:22
  • @DanielLovasi `question` is a local variable from the function "validate_input" after a value is set into the variable the function will return the value, so when we call to our function like `net_module = validate_number("Please mark for the Networking module: ")` we are returning an int an set it to `net_module` after the function is done – Joaquín Feb 12 '21 at 17:42
0
allStudents = []
NumberOfStudent = 2

while len(allStudents) < NumberOfStudent:
        oneStudent = []
        try:
            oneStudent.append(input("Please input student name: ",))
            oneStudent.append(int(input("Please mark for the Web module: ",)))
            oneStudent.append(int(input("Please mark for the Programming module: ",)))
            oneStudent.append(int(input("Please mark for the Graphics module: ",)))
            oneStudent.append(int(input("Please mark for the Networking module: ",)))
            allStudents.append(oneStudent)
        except:
            print('Enter valid input')
print(allStudents)
Ankit Sangwan
  • 1,138
  • 1
  • 7
  • 20
  • Ankit, with your last edit you forgot to change len(studentDetails) to allStudents as well as the print statement. – JackTheCrab Feb 12 '21 at 17:16