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