-2

I'm doing a small program that has the job of a school's subjects and marks management.

HOW IT WORKS

I've created 4 variables: a dictionary, an initialized empty string(for subjects) and two initialized empty integers(one for the marks, the other one for the subjects number). The program asks the user to input the numbers of subjects to work with. After that, it enters in a for loop that repeats itself in the range of the subjects number. And it works fine. Now the problem comes when the user inputs a mark over the range specified. Let's suppose we have a school and the marks we can input are between 1 and 10. The user can't write 11,12 and so on..

CODE

pagella = {}
materia = ""
voti = 0
length = 0

print("Insert the number of subjects, the subjects and the marks-")
num = int(input("Number of subjects: "))
length = len(pagella)
length = num

for i in range(length):
    materia = input("Subject: ")
    voto = eval(input("Mark: "))
    if(voto > 10):
        print("Number between 1 and 10")
    else:
        pagella[materia] = voto
print(pagella)

As you can see, there's the for loop that asks the user to input the subjects and the marks in the range of num(the number of subjects). If I try to input 12 on a mark, the program tells me to input a mark between 1 and 10 so it repeat again the cycle from the beginning. But the problem is that in this case, if I want to have 3 subjects with 3 marks (that means 3 iterations), one is lost because of the wrong mark, so I will not have 3 subjects and marks to input correctly, but only 2.

I hope I've explained correctly everything and someone can help me!

Thanks in advice

aghin00
  • 3
  • 1
  • 1
    Possible duplicate of [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) – Mike Scotty Mar 13 '18 at 13:22
  • But in that case he doesn't use a cycle – aghin00 Mar 13 '18 at 13:25
  • Then put a "cycle" around the part where the user is asked? Or even better, put the asking part into a function and call that function in your loop. – Mike Scotty Mar 13 '18 at 13:26
  • I've tried, but it didn't work. What I also tried was to increment the length of the cycle by 1 if the first condition is true, but it didn't work, too. I didn't try the function case, now I'll give it a try and let you know – aghin00 Mar 13 '18 at 13:30

1 Answers1

0

I suggest you the following solution that handles also the cases of non-numeric values as input for marks:

pagella = {}
materia = ""
length = 0

print("Insert the number of subjects, the subjects and the marks-")
num = int(input("Number of subjects: "))
length = num

for i in range(num):
    # Ask for subject
    materia = input("Subject: ")
    # Ask for mark until the input is correct
    voto = None
    while (voto is None):
        try:
            voto = int(input("Mark: "))
            if (voto > 10):
                print("Number between 1 and 10")
                voto = None
        except:
            print("Pleaser enter an integer")
    # Store in dictionary pagella (if mark is correct)
    pagella[materia] = voto

print(pagella)
Laurent H.
  • 6,316
  • 1
  • 18
  • 40