Python Forum

Full Version: How can I make a sentinel value NOT be initialized in a class/method - OOP?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am testing out some basic Object Oriented Programming in Python.  The basics:
  • User enters a name
  • While loop with a sentinel value of "quit" will continue entering names until the sentinel value is reached
  • The object is created with the inputted value (NAME and until a sentinel value is entered)
  • the object is then printed out using the constructor __str__

in my solution ( I am saying this because someone might have a work around that doesn't include the following below)
  • I want to use __init__
  • I want to use __str__
  • I want to use a while loop with a sentinel value
EXPLANATION: 
I want to have the user enter in their NAME using a while loop, with "quit" being the sentinel value that breaks the loop.  Once the name is entered, an object is created and it is passed into a constructor def __ini__ (self, name) and then I have them return a string value using __str__ for the name entered.
The issue I am having is that when i enter the sentinel value of QUIT, it gets initialized as the name and printed out.  How can I get around this?  I hope this makes sense.
It's not making a lot of sense to me. Can you show us the relevant parts of your code?
Not completely sure, but I think you want:
while True:
    name = input()
    if name == 'quit':
        break
    # do stuff with name