0

Why does my dictionary was accepting value when I have input a valid key and invalid value at first I saw it was an error but when I finish the program when my program display the dictionary the error value at first was printed too how can I fix this? is it possible to do it while in while loop? so what am I going to do?

students={}

count = 1
while count<=3:
    studNum=input("Enter student number " + str(count) + " : ")
    studFname=input("Enter first name " + str(count) + " : ")
    if studNum=='' or studFname=='':
        print("Invalid Please input a valid data, try again....")
        count-=1
        pass
    elif studNum in students.keys():
        print("That student number is already used, try again...")
        count-=1
    count = count+1
    students[studNum] = studFname
    if count==4:
        print("\nStudent List:")
        for x,y in students.items():
            print(x,y)
            pass
        enter=input("\nPlease hit ENTER to delete the 3rd entry")
        if enter=='':
            print("Processing...\n")
            del students[studNum]
            print("3rd entry has been deleted!\n")
            count-=1
            while count<=3:
                studNum1=input("Enter student number " + str(count) + " : ")
                studFname1=input("Enter first name " + str(count) + " : ")
                if studNum1=='' or studFname1=='':
                    print("Invalid Please Try again....")
                    count-=1
                    pass
                elif studNum1 in students.keys():
                    print("That student number is already used, try again...")
                    count-=1
                count = count+1
            students[studNum1] = studFname1
            print("\nStudent List:")
            for x,y in students.items():
                print(x,y)
                pass
            pass
        else:
            print("Invalid Please Try again....")
    pass

Output:

Enter student number 1 : 123
Enter first name 1 :
Invalid Please input a valid data, try again....
Enter student number 1 : 123
Enter first name 1 : a
That student number is already used, try again...
Enter student number 1 : 1234
Enter first name 1 : a
Enter student number 2 : 12345
Enter first name 2 : b
Enter student number 3 : 123456
Enter first name 3 : c

Student List:
123 a
1234 a
12345 b
123456 c

Expected Output:

Enter student number 1 : 123
Enter first name 1 :
Invalid Please input a valid data, try again....
Enter student number 1 : 123
Enter first name 1 : a
Enter student number 2 : 1234
Enter first name 2 : b
Enter student number 3 : 12345
Enter first name 3 : c

Student List:
123 a
1234 b
12345 c
Neucro
  • 252
  • 2
  • 6

2 Answers2

0

All you need to do is skip the current step in the loop in case you dont want to insert the "bad" values:

if studNum == '' or studFname == '':
    print("Invalid Please input a valid data, try again....")
    continue
elif studNum in students.keys():
    print("That student number is already used, try again...")
    continue

You didn't skip the line students[studNum] = studFname, thus the insertion.

In this case, you don't want to decrement the count, because you do not add to it at all.

p.s: Read here about pass: how-to-use-the-pass-statement

Shimon Cohen
  • 489
  • 3
  • 11
0

You can check if this logic works for you

students={}
count = 1
while count<=3:    
    studNum=input("Enter student number " + str(count) + " : ")    
    studFname=input("Enter first name " + str(count) + " : ")    
    if (studNum=='' or studFname=='') or (studNum in students.keys()):    
        while(studNum=='' or studFname=='') or (studNum in students.keys()):   
            print("Invalid Please input a valid data, try again....")    
            studNum=input("Enter student number " + str(count) + " : ")    
            studFname=input("Enter first name " + str(count) + " : ")    
            if (studNum=='' or studFname=='') or (studNum in students.keys()):    
                pass    
            else: 
                students[studNum] = studFname    
                break
        count = count+1
    else:
        students[studNum] = studFname
        count = count+1
    if count==4:
        print("\nStudent List:")
        for x,y in students.items():
            print(x,y)
            pass
        enter=input("\nPlease hit ENTER to delete the 3rd entry")
        if enter=='':
            print("Processing...\n")
            del students[studNum]
            print("3rd entry has been deleted!\n")
            count-=1
            while count<=3:
                studNum1=input("Enter student number " + str(count) + " : ")
                studFname1=input("Enter first name " + str(count) + " : ")
                if studNum1=='' or studFname1=='':
                    print("Invalid Please Try again....")
                    count-=1
                    pass
                elif studNum1 in students.keys():
                    print("That student number is already used, try again...")
                    count-=1
                count = count+1
            students[studNum1] = studFname1
            print("\nStudent List:")
            for x,y in students.items():
                print(x,y)
                pass
            pass
        else:
            print("Invalid Please Try again....")
    pass