Hello all, for this code I am asking the user to input a new vehicle to be added to the dictionary. However, if I input an invalid rental rate (eg. e4) it will immediately raise the valueerror. How can I make it so even if an invalid input in entered, the code will still prompt me for the VIN code before raising the valueerror? thank you!
#π Prompt user for input even if previous input is invalid?
32 messages Β· Page 1 of 1 (latest)
@brisk dew
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
what I always do is: I write a separate function named something like "get_valid_input". It does what it says: it loops until it gets input that we consider valid, and only then does it return it.
gimme a second, I'll show you an example
this isn't exactly your situation, but it's vaguely similar: ```py
def get_string_from_choices(prompt: str, choices: list[str]) -> str:
while True:
datum = input(prompt).strip()
if datum in choices:
return datum
print(f"{datum} isn't in {choices}; please try again")
perhaps a closer analog is this ```py
def get_int(prompt: str) -> int:
while True:
try:
return int(input(prompt))
except ValueError as e:
print(f"{e}; please try again")
hmm it is slightly similar, but in this case it isn't a must to have a valid input
it isn't?
but you're catching the same exception.
post your code as text and I'll show you
nope, all I wish to change is so all 3 questions are asked despite the user input
ah alright
if choice == "1":
try:
model = input("Enter vehicle model: ")
rental_rate = float((input("Enter rental rate: ")))
vin = input("Enter VIN: ")
company.add_vehicle(Vehicle(model, rental_rate, vin))
print("Vehicle added successfully!")
except ValueError:
print("Vehicle could not be added.")
whoops the formatting is a bit off
like this ```py
def add_vehicle_model() -> str:
while True:
try:
model = input("Enter vehicle model: ")
rental_rate = float((input("Enter rental rate: ")))
vin = input("Enter VIN: ")
company.add_vehicle(Vehicle(model, rental_rate, vin))
return
except ValueError as e:
print(f"Vehicle could not be added because {e}")
this is a bit annoying, since it always asks for both model and VIN, even if they only got one of those two things wrong.
You can also convert to a float only after asking the questions
yes lol
In pseudo code:
try:
model =input
rental_rate =input
vin=input
rental_rate -> float
except:
...```
yes I've tried that and unfortunately it doesn't really work
for this code it works but the VIN is asked after the exception is printed which might be weird
What did you try with and how doesn't it work?
if choice == "1":
while True:
try:
model = input("Enter vehicle model: ")
rental_rate = (input("Enter rental rate: "))
vin = input("Enter VIN: ")
rental_rate = float(rental_rate)
company.add_vehicle(Vehicle(model, rental_rate, vin))
print("Vehicle added successfully!")
return
except ValueError:
print("Vehicle could not be added.")
in this case the vin is still asked after the exception is raised π€
that's weird
that should work
I think there was something off with the initial input for rental rate, I added rental_rate = ββ and it ended up working
thank you everyone!
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.
π Prompt user for input even if previous input is invalid?