def PrintBlue():
print (" You chose Blue!\r\n")
def PrintRed():
print (" You chose Red!\r\n")
def PrintOrange():
print (" You chose Orange!\r\n")
def PrintYellow():
print (" You chose Yellow!\r\n")
#Let's create a dictionary with unique key
ColorSelect = {
0:PrintBlue,
1:PrintRed,
2:PrintOrange,
3:PrintYellow
}
Selection = 0
while (Selection != 4):
print ("0.Blue")
print ("1.Red")
print ("2.Orange")
print ("3.Yellow")
try:
Selection = int(input("Select a color option: "))
x=0
if ( Selection < 0) and (Selection > 3):
raise KeyError(" Enter a number >=0 and <4)")
else:
ColorSelect[Selection]() # Run the function inside dictionary as well
except KeyError:
pass
Above is my python code. I am using 2.7 version. but after running I got different result for input =4. While I am expecting same result for Selection<0 or >3.Here is result look like:
0.Blue 1.Red 2.Orange 3.Yellow Select a color option: 5 0.Blue 1.Red 2.Orange 3.Yellow Select a color option: 4
Notice after I enter input =4 , python exits from run time.When I enter 0,1,2,3,5,6,7, every time it ask again to enter value again, but when I enter 4 it exits.
Aucun commentaire:
Enregistrer un commentaire