Python Forum
Class and Object HW for cars - Help with error handling for user inputs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class and Object HW for cars - Help with error handling for user inputs
#1
Hi All,

I have this HW problem that deals with classes and objects and I think i did the assignment correctly from the instructions. However, I wish to add error handling and I am not sure the best way to go about this. I did a try/except to let the user know about the error but I don't think this is the best way with user inputs. I would like guidance on how to use error handling to have the user keep inputting until the entry is valid. I am imagining scenarios where the user inputs numbers for string inputs and vice versa.

Attaching my HW assignment instructions and my codes.

Thanks!
Jake

Attached Files

.pdf   class Car HW problem.pdf (Size: 77.8 KB / Downloads: 20)
.py   car_object_error_handling.py (Size: 1.38 KB / Downloads: 13)
.py   Car.py (Size: 1.19 KB / Downloads: 15)
Reply
#2
Please post the instructions and the code as text, not links.

Method names should not have underscores. Use def acceleration(self), not def __acceleration__(self). Double underscores at the front and end of a method name has a special meaning in Python. __init__() is one of the special methods that uses double underscores (dunders).

I would not use double underscores in front of attribute names either. I think the instructions to use __year and __make instead of year, speed and make display some ignorance of what double underscores at the front of an instance variable mean. Names like __speed should only be used when you are instructed to do so by your teacher, or when a class does not want subclasses to inherit the attribute. For "private" attributes that are not part of the class interface, use a single underscore in front of the name.

I looked at car_object_error_handling.py. The only possible error you can have in your program is entering a non-numeric number year. You should limit your program to only catching that error, not wrapping all of main() in a generic try/except. There is no error that catches if you entered number when you should enter letters.

Nowhere in the instructions does it say you heed to validate input. Nowhere in the instruction does it even say that year has to be a number.

I also noticed that this is wrong:
print(f"The {year} {make}'s current speed is {myCar.__get_speed__()}")
The instructions do not say "print the year and make and a message. The instructions show the output as:
Output:
5 10 15 20 25 20 15 10 5 0
Reply
#3
Thanks for the gudiance!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pass an object to a class, then make an object of it and pass again TomasAm 11 4,669 Nov-09-2020, 04:47 PM
Last Post: buran
  class method on two object gabejohnsonny21 4 2,506 Apr-22-2020, 06:57 AM
Last Post: DeaD_EyE
  Handling IO Error / Reading from file Expel 10 4,990 Jul-18-2019, 01:21 PM
Last Post: snippsat
  Class example -i don't understand the error sonedap 9 4,366 Feb-02-2019, 04:36 PM
Last Post: sonedap
  Integer object error Zatoichi 2 3,794 Feb-18-2018, 07:25 AM
Last Post: Zatoichi
  Is there a specialist in error handling ? sylas 9 6,220 May-22-2017, 03:17 PM
Last Post: micseydel
  error 'ring' is not a callable object JakeWitten 1 3,112 May-02-2017, 05:09 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020