Issues With Lists? Error Checking Not Working
Solution 1:
You don't have a list, you are testing characters against one long string:
possible_types = " ".join(possible_types)
The letters h
and s
are in that string (in the words High_Economy
and Business
, respectively), but the sequence try
doesn't appear anywhere in the string.
If you only wanted to allow whole words to match, you'd need to leave possbile_types
a list, or ideally convert it to a set (as sets allow for fast membership testing). You can define the list, no need for list.extend()
here:
possible_types = ["Low_Economy", "Standard_Economy", "High_Economy",
"Business", "First", "Residence"]
or make it a set by using {...}
:
possible_types = {"Low_Economy", "Standard_Economy", "High_Economy",
"Business", "First", "Residence"}
Do not join this into a string, just test directly against the object:
ifself.seat notin possible_types:
If you still need to show the values to a user in an error message, join the values then, or store the str.join()
result in a different variable for that purpose.
Note that you shouldn't deal with user input validation in the class __init__
method. Leave user interaction to a separate piece of code, and create instances of your class after you validated. That way you can easily swap out user interfaces without having to adjust all your data objects too.
Solution 2:
possible_types = " ".join(possible_types)
Above statement will create one string as "Low_Economy Standard_Economy High_Economy Business First Residence".
Now you are doing
ifself.seat notin possible_types:
This will check for a particular character in the string present or not. In your case you are finding 'h' which is present and 'try' which isn't.
Your program will work if you remove this statement
possible_types = " ".join(possible_types)
Post a Comment for "Issues With Lists? Error Checking Not Working"