I am not sure if I am using Pydantic incorrectly but I want users to type in a terminal and if they get it wrong, I want them to keep trying until entering a valid range.
class Input(BaseModel):
number_range:str
name:str
@validator("number_range")
def all_numbers_are_valid(cls, value:str) -> str:
string_list:list = value.split(",")
string_list = list(map(cls.remove_spaces, string_list))
string_bool = list(map(lambda x: x.isnumeric(), string_list))
if False in string_bool:
flag = True
while flag:
print("Invalid characters, please try again.")
value = input("Range of floors:")
string_list:list = value.split(",")
string_list = list(map(cls.remove_spaces, string_list))
string_bool = list(map(lambda x: x.isnumeric(), string_list))
flag = True if False in string_bool else False
return ", ".join(map(str,string_list))
@validator("name")
def no_space_name(cls, value:str) -> str:
return cls.remove_spaces(value)
Input(number_range=input("Enter a range of numbers:"), name=input("Enter name:"))
Example of input:
Enter a range of numbers: 0,1,a
Enter name: he llo
I want user to be prompted with entering the a valid range of numbers BEFORE moving onto next field. Is that possible?