Python Forum
How can I make a sentinel value NOT be initialized in a class/method - OOP?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I make a sentinel value NOT be initialized in a class/method - OOP?
#1
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.
Reply
#2
It's not making a lot of sense to me. Can you show us the relevant parts of your code?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Not completely sure, but I think you want:
while True:
    name = input()
    if name == 'quit':
        break
    # do stuff with name
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class definition and problem with a method HerrAyas 2 243 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  super() and order of running method in class inheritance akbarza 7 725 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Using one child class method in another child class garynewport 5 1,578 Jan-11-2023, 06:07 PM
Last Post: garynewport
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,311 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,461 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,762 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,702 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  anonymous method in a class Skaperen 8 3,575 May-23-2021, 11:17 PM
Last Post: Skaperen
  How to apply a class method to an entire dataframe column tirtha9 1 5,130 Jan-03-2021, 04:44 AM
Last Post: klllmmm
  NameError when calling a class method mfreudenberg 2 2,301 Sep-25-2020, 07:40 AM
Last Post: mfreudenberg

Forum Jump:

User Panel Messages

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