I am trying to create a loop that matches user entered input to a key from defined dictionary and returns the corresponding value as an output. However, when the user inputs something that is not in keys, the program prints None and it terminates. But I want the program to continue to let the user enter an input until it finds a match on one of the keys, and it should terminate only when user has provided an input key that is in present the dictionary. I am new to Python so am not sure how to use while statement to loop the process. Appreciate any help.
list_dict = {"James" : "Pen Set", "Tim" : "Geometry Box", "Rocky" : "Sticky Notes" , "Jeff" : "Alarm Clock" , "Sandy" : "Highlighters"}
_name = input("Enter Name: ")
x = list_dict.get(_name)
while x == None:
_name = input("Re-Enter Name: ")
x = list_dict.get(_name)
break
else:
print(_name, "got", x)
break
CodePudding user response:
You are using the break
keyword directly inside a while-loop without any condition. The result is that the loop content can only run once before exiting the loop. If you wish to keep the loop running, remove the break
keyword.
Here is a sample answer to your question:
list_dict = {"James" : "Pen Set", "Tim" : "Geometry Box", "Rocky" : "Sticky Notes" , "Jeff" : "Alarm Clock" , "Sandy" : "Highlighters"}
# first input
_name = input("Enter Name: ")
# obtain x
x = list_dict.get(_name)
# perform checking on the latest value of x
while x == None:
_name = input("Re-Enter Name: ")
x = list_dict.get(_name)
# if x is a valid value, the while statement above will detect this and terminate the loop
# final output
print(_name, "got", x)
Remark 1: keep in mind that loops can be terminated automatically by setting the loop condition correctly. Arbitrary use of break
and continue
keywords is generally advised against due to its disruption to normal programme flow.
Remark 2: using else
keyword with while
keyword on the same level means that the else
block is only executed if the while-loop completes its execution without any interruptions, i.e. never encountering the break
keyword. This is inapplicable to your case, since the termination condition is simple, and there is no need to detect if the while-loop is interrupted.
CodePudding user response:
The problem is the break statement. If you remove it, the program should work fine ;)
CodePudding user response:
The provided code only requires a minor change to produce the behavior that you expect. In the following modified code, both break
statements have been removed:
list_dict = {"James" : "Pen Set", "Tim" : "Geometry Box", "Rocky" : "Sticky Notes" , "Jeff" : "Alarm Clock" , "Sandy" : "Highlighters"}
_name = input("Enter Name: ")
x = list_dict.get(_name)
while x == None:
_name = input("Re-Enter Name: ")
x = list_dict.get(_name)
# break
else:
print(_name, "got", x)
# break
Explanation
- Remove the first
break
to prevent thewhile
loop from always exiting after the first iteration. If the user-provided name does not exist inlist_dict
, we want thewhile
loop to continue iterating. By placing abreak
, we are forcing thewhile
loop to exit after the first iteration. It's also important to note that abreak
causes anelse
clause to be skipped. With the original code, if the first name entered does not exist, but the second name entered does exist, thebreak
causes theelse
clause to be skipped and theprint
statement is not executed. - Remove the second
break
because it is only applicable when placed inside a loop.
Refer to the documentation for the break
statement for additional information.
Testing the modified code
Enter Name: TestName1
Re-Enter Name: TestName2
Re-Enter Name: Jeff
Jeff got Alarm Clock
CodePudding user response:
I improved a little of your code and i think this help to solve your problem.
list_dict = {"James" : "Pen Set", "Tim" : "Geometry Box", "Rocky" :
"Sticky Notes" , "Jeff" : "Alarm Clock" , "Sandy" : "Highlighters"}
_name = input("Enter Name: ")
while _name not in list_dict.keys():
_name = input("Re-Enter Name: ")
print(f'{_name} got {list_dict.get(_name)}')