-1

i'm trying to compare a user input to a dic in python, the program should take the student id as an input, and return the student grade. and the program should follow some rules: 1- the length of the input must not be less than OR greater than 5, if so the program should return an error message. 2- if the ID isn't in the dic the program should also send an error message. i wrote the code below and when the input length is less than or greater than 5 the code works, but when the input length == 5 i get a looping message that says the ID not found even if the id is in the dic. the code :

students = {11111: "A+", 22222: "B+", 33333: "D+"}
ID = input("please enter the student ID:")
while len(str(ID)) == 5:
    for key in students:
        if ID == key:
            print(students[int(ID)])
        else:
            print("ID not found")
if len(str(ID)) < 5:
    print("invalid Id")
elif len(str(ID)) > 5:
    print("invalid Id")
Omar
  • 1
  • 2
    Welcome and happy Friday! You need to put your `ID` definition inside the loop too, otherwise it never changes and the `while` condition is never satisfied – Chris_Rands Jan 21 '22 at 19:54
  • 1
    `ID` is a string and the keys are ints. `if ID == key:` will ***never*** be true... Why not just `print(students.get(int(ID), "ID not found")`? No need for a loop... – Tomerikoo Jan 21 '22 at 19:55
  • Does this answer your question? [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) – Tomerikoo Jan 21 '22 at 19:56

3 Answers3

0

You will want to save the input as an int to compare:

students = {11111: "A+", 22222: "B+", 33333: "D+"}
ID = int(input("please enter the student ID:"))
for key in students:
    if ID == key:
        print(students[int(ID)])
        break
    else:
        print("ID not found")
if len(str(ID)) < 5:
    print("invalid Id")
elif len(str(ID)) > 5:
    print("invalid Id")

That will let you compare correctly, but I think a better version would be the following:

students = {11111: "A+", 22222: "B+", 33333: "D+"}
ID = int(input("please enter the student ID:"))
found = False
if ID in students:
    print(students[ID])
    found = True
if not found:
    print("ID not found")
if len(str(ID)) != 5:
    print("invalid Id")
Eli Harold
  • 2,280
  • 1
  • 3
  • 22
0

I prefer this code to you:

students = {11111: "A+", 22222: "B+", 33333: "D+"}

ID = int(input("please enter the student ID: "))
if len(str(ID)) == 5:
    if ID in students.keys():
        print(students[ID])
    else:
        print('ID no found')
else:
    print("invalid ID")

It first check the length of the input, then if your input be in the dictionary it prints the response, else prints "ID not found".

-1

First of all, you should follow Asking the user for input until they give a valid response regarding your input-loop. It should be something like this:

while True:
    ID = input("please enter the student ID:")
    if len(ID) != 5:
        print("invalid Id")
    else:
        # check the id
        break

Now, regarding how you check the keys - it is not necessary to loop over a dict to check if a key exists. The whole advantage of dicts is that they are hash tables and give an O(1) look-up time. So you can simply do:

if ID in students:
    print(students[ID])
else:
    print("ID not found")

But since you're just printing, this can all be simplified using the get method which has a default argument that is returned if the key is not found. So an if/else is not even necessary:

print(students.get(ID, "ID not found"))

Lastly, remember that input always returns a string. Your keys are ints. So you will have to convert the ID to an int before using it as a key.

All together your code could be:

while True:
    ID = input("please enter the student ID:")
    if len(ID) != 5:
        print("invalid Id")
    else:
        print(students.get(int(ID), "ID not found"))
        break
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61